Sunday, January 13, 2008

How do I load text or csv file data into SQL Server with BULK INSERT in TSQL

How do I load text or csv file data into SQL Server?
BULK INSERT examples to import CSV / TSV files into SQL Server.

CSV file
BULK INSERT OrdersBulk
FROM 'c:\file.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
TSV file, tab-separated values
BULK INSERT OrdersBulk
FROM 'c:\file.csv'
WITH
(
FIELDTERMINATOR = '\t',
ROWTERMINATOR = '\n'
)
---

BULK INSERT OrdersBulk
FROM 'c:\file.csv'
WITH
(
FIELDTERMINATOR = '\t',
ROWTERMINATOR = '\t\n'
)
FIRSTROW parameter
BULK INSERT OrdersBulk
FROM 'c:\file.csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
---



BULK INSERT OrdersBulk
FROM 'c:\file.csv'
WITH
(
FIRSTROW = 2,
MAXERRORS = 0,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
---

No comments: