SQL Server 2012 Import CSV to mapped Columns

蹲街弑〆低调 提交于 2019-12-10 16:38:39

问题


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.



来源:https://stackoverflow.com/questions/11016223/sql-server-2012-import-csv-to-mapped-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!