Solved

Import CSV into SQL2008 using TSQL

Posted on 2009-06-27
2
638 Views
Last Modified: 2013-11-10
Experts,
I have a need to import a csv file that has the first row as column names into a table in an SQL2008 database. This must be done through the use of TSQL. This table will contain over 100 columns. The destination table column datatypes are pretty varied.

This could be done with a dtsx package that is executed from TSQL however I am running into truncation validation issues with most of the datatypes of the columns.

Thanks in advance!
TC72
0
Comment
Question by:TC72
2 Comments
 
LVL 7

Accepted Solution

by:
Mohed Sharfi earned 500 total points
ID: 24727975
Hi TC72,
How are you doing,to do this Please try this Example T-SQL(Snnipet) to  import from a csv file.
thanks
SELECT cast(field1 as bigint) as field1,cast(field2 as varchar(15)) as field2 
INTO dbo.file1table2
FROM 
OPENROWSET('MSDASQL','Driver={Microsoft Text Driver (*.txt; *.csv)};
DefaultDir=X:\;', 
'SELECT * from X:\Test\Import_OpenRowset.csv')
Select * from dbo.file1table2;

Open in new window

0
 

Author Comment

by:TC72
ID: 24728266
Thank you for your quick reply!

This worked well after enabling 'Ad Hoc Queries' on the DB instance.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question