My Technical Notes

Tuesday, 22 March 2016

SQL Server: Working Example of a CSV bulk Import

CSV File

The CSV file content is the following:


Forename,Surname
Tahir,Hassan
Jon,Doe

It's encoding, in Notepad++, is `Encode in UTF8`.

SQL Table Creation

The following code creates the `dbo.TestTable` table:


CREATE TABLE dbo.TestTable
(
    [Forename] nvarchar(max),
    [Surname] nvarchar(max)
)

Bulk Inserting the Data

The following script inserts the data:


BULK INSERT dbo.TestTable
FROM 'C:\Temp\testCsv.txt'
WITH    
(
    FIRSTROW = 2,
    FIELDTERMINATOR = ',',
    ROWTERMINATOR = '\n'
)
GO

The `GO` at the end is really a signal for SSMS is superfluous if we execute that block ourselves.

No comments: