Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Load data from text file with variable columns

By Edgardo Valdez,

This script loads from a formatted text file (E.g. tab delimited) with variable number of columns using BULK INSERT. For instance, the file may have any number of columns and next load (seconds later) the same file may have a different number of columns.

This script reads the first line (column names) extracting the names and using them to create a table dynamically appending the time stamp string to the table name (to ensure uniqueness). After executing the CREATE TABLE directive, and creating a new table, it will execute the second BULK INSERT to load the data to the new table.

The table definition uses varchar(1000) for all the columns to make the load easier. I would suggest to perform data validation the load is complete.

Total article views: 924 | Views in the last 30 days: 4
Related Articles

Create table with number of columns in a parameter

Possible to create a table with a @param containing the number od columns to create?


Pivot with dynamic number of columns

Pivot with dynamic number of columns


Create a new column with criteria

Create serial numbers for sub category items


Creating Dynamic Columns in SSRS 2008

Creating Dynamic Columns in SSRS 2008


BULK INSERT - Inconsistent number of columns in DAT file

Help required to build from a DAT file with inconsistent number of columns to a fixed number of colu...

data warehousing    

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones