SHARE
Facebook X Pinterest WhatsApp

Load TXT File using OPENROWSET function

Feb 23, 2006

>>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 :

[dts_2_11012005.txt]
ColNameHeader=True
Format=TabDelimited
MaxScanRows=0
CharacterSet=ANSI
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 * INTO txtmport FROM
OPENROWSET(”Microsoft.Jet.OLEDB.4.0”,
”Text;Database=C:STUDIESsql_serveropenrowset_txt_to_sql”,
”SELECT * FROM dts_2_11012005.txt”) 

Download Script:



Disclaimer:
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

Recommended for you...

What Backups Do I Have?
Gregory Larsen
May 12, 2021
Improving the Performance of a Table Variable using Optimizer Hint RECOMPILE
Gregory Larsen
Apr 1, 2021
TYPE Definition Change in Oracle 21c
Tip 74 – Changing Cost Threshold for Parallelism
Gregory Larsen
Feb 24, 2021
Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.