Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Import CSV into SQL2008 using TSQL

Posted on 2009-06-27
2
Medium Priority
?
644 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
Mohed Sharfi earned 2000 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

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.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

721 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