Solved

Import CSV into SQL2008 using TSQL

Posted on 2009-06-27
2
637 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Backing up an SQL Transaction Log 11 42
Sql Count with Select Distinct 4 27
Sql Query Datatype 2 19
SQL 2008 R2 calc date formula 3 28
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now