snippetsqlModerate
How to import CSV file in SQL server 2008?
Viewed 0 times
file2008sqlcsvhowserverimport
Problem
I am trying to import a CSV file in SQL Server 2008.
What would be an alternative way to achieve this goal?
Any thoughts/ideas much appreciated.
BULK INSERT is a way to go but it is applicable for CSV from SQL Server 2014 onwards.What would be an alternative way to achieve this goal?
Any thoughts/ideas much appreciated.
Solution
SQL Server has always supported bulk inserting from CSV files, you just have to specify field/row terminators.
file.csv contains:
Then we do this:
Results:
file.csv contains:
foo,bar,1
blat,splunge,2Then we do this:
CREATE TABLE #foo(a varchar(32), b varchar(32), c int);
BULK INSERT #foo FROM 'c:\temp\file.csv'
WITH (ROWTERMINATOR = '\n', FIELDTERMINATOR = ',');
SELECT * FROM #foo;Results:
a b c
-------- -------- ----
foo bar 1
blat splunge 2Code Snippets
foo,bar,1
blat,splunge,2CREATE TABLE #foo(a varchar(32), b varchar(32), c int);
BULK INSERT #foo FROM 'c:\temp\file.csv'
WITH (ROWTERMINATOR = '\n', FIELDTERMINATOR = ',');
SELECT * FROM #foo;a b c
-------- -------- ----
foo bar 1
blat splunge 2Context
StackExchange Database Administrators Q#221817, answer score: 10
Revisions (0)
No revisions yet.