Load TXT File using OPENROWSET function

>>Script Language and Platform: T-SQL/ SQL SERVER

Let’s say we have txt file dts_2_11012005.txt.
The file is located in subdirectory C:STUDIESsql_serveropenrowset_txt_to_sql.

dts_2_11012005.txt is a Tab Delimited file, which contains following columns :

emp_id       Width 9
fname        Width 8
minit        Width 1
lname        Width 9
job_id       Width 2
job_lvl      Width 3
pub_id       Width 4
hire_date    Width 10

To load this file in SQL Database we need to create Schema.ini file in
The same subdirectory : C:STUDIESsql_serveropenrowset_txt_to_sql.
Detailed description of Schema.ini file you can find in SQL SERVER book online.

Schema.ini file for dts_2_11012005.txt should look like :

Col1=emp_id Text Width 9
Col2=fname Text Width 8
Col3=minit Text Width 1
Col4=lname Text Width 9
Col5=job_id Text Width 2
Col6=job_lvl Text Width 3
Col7=pub_id Text Width 4
Col8=hire_date date Width 10

Now we can run from Query Analyzer the code below:

Author: Boris Bondar

Use pubs
”SELECT * FROM dts_2_11012005.txt”)

Download Script:

We hope that the information on these script pages is
valuable to you. Your use of the information contained in these pages,
however, is at your sole risk. All information on these pages is provided
“as -is”, without any warranty, whether express or implied, of its accuracy,
completeness, or fitness for a particular purpose…

Disclaimer Continued

Back to Database Journal Home

Get the Free Newsletter!

Subscribe to Cloud Insider for top news, trends & analysis

Latest Articles