SQL Server 2012 Import CSV to mapped Columns

2019-06-28 00:43发布

问题:

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

回答1:

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 


回答2:

If you are ready to use a tool to do this you can use the SQL Server Import and Export Wizard. You can start the SQL Server Import and Export Wizard from the Start menu, from SQL Server Management Studio, from SQL Server Data Tools (SSDT), or at the command prompt. You can map the destination and source column very easily using this tool. Later on if u wish to update the other column you can do using code.