SQL Server 2012 Import CSV to mapped Columns

D-W picture D-W · Jun 13, 2012 · Viewed 10.4k times · Source

I'm currently trying to import about 10000 rows (from a CSV file) into an existing table.

I only have 1 column which I'm trying to import, but in my table I have a another column called TypeId which I need to set to a static value i.e. 99E05902-1F68-4B1A-BC66-A143BFF19E37.

So I need something like

INSERT INTO TABLE ([Name], [TypeId])
Values (@Name (CSV value), "99E05902-1F68-4B1A-BC66-A143BFF19E37")

Any examples would be great.

Thanks

Answer

praveen picture praveen · Jun 13, 2012

As mentioned above import the data into a temporary table and then insert the value into the actual table

DECLARE @TempTable TABLE (Name nvarchar(max))

 BULK INSERT @TempTable 
 FROM ‘C:\YourFilePath\file.csv’
 WITH ( FIELDTERMINATOR = ‘,’,
 ROWTERMINATOR = ‘\n’
)

INSERT INTO TABLE ([Name], [TypeId])
Select Name,'99E05902-1F68-4B1A-BC66-A143BFF19E37' from @TempTable