Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Tips Database Forum Rss Feed

» Database Journal Home
» Database Articles
» Database Tutorials
MS Access
SQL Scripts & Samples
» Database Forum
» Slideshows
Free Newsletters:

News Via RSS Feed

Rss Feed

Database Journal |DBA Support |SQLCourse |SQLCourse2

Featured Database Articles

SQL Scripts & Samples

Posted Feb 23, 2006

Load TXT File using OPENROWSET function

By DatabaseJournal.com Staff

>>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 * INTO txtmport FROM 
''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

SQL Scripts & Samples Archives

Latest Forum Threads
SQL Scripts & Samples Forum
Topic By Replies Updated
sql help!! add 1 August 22nd, 11:58 AM
How To Combine These 2 Queries into 1 Query ? tarek_land 1 June 7th, 08:37 AM
solving query svibuk 1 February 3rd, 06:08 AM
converting from a character string to uniqueidentifier saturnius 4 January 4th, 05:56 AM