Solved

Import CSV into SQL2008 using TSQL

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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

760 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