英文:
BULK INSERT/UPDATE statement syntax error?
问题
我正在尝试将CSV文件中的数据批量插入或更新到SQL数据库中的现有表格,但我一直收到错误信息“关键字'BULK'附近的语法不正确”,我已经尝试了谷歌上能找到的一切方法。
以下是我正在使用的代码:
CREATE VIEW [temp_table] AS
SELECT column_1, column_2...
FROM table;
BULK UPDATE [temp_table]
FROM 'C:\FileName.csv'
WITH (
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
);
我已经尝试过使用UPDATE和INSERT。
我尝试直接导入数据,但出现了一堆权限错误,我无法解决。
我尝试直接从CSV文件进行操作,但遇到了一个问题,即数据库中有一个ID列,但CSV中没有,而ID列会自动生成数字。
任何建议都将不胜感激。
英文:
I am trying to bulk insert or update the data from a csv file to a pre-existing table in a database in SQL and I keep getting the error "Incorrect syntax near keyword 'BULK'" and I've tried everything I can find on google.
Here is the code I am working with:
CREATE VIEW [temp_table] AS
SELECT column_1, column_2...
FROM table;
BULK UPDATE [temp_table]
FROM 'C:\FileName.csv'
WITH(
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
);
I have tried both UPDATE and INSERT.
I have tried importing the data directly but that just came up with a load of permission errors that I cannot get round.
I tried doing it directly from the csv file but it runs into the issue that the id column is on the database but not on the csv and the id column automatically generates numbers.
Any advise is appreciated.
答案1
得分: 1
我更喜欢使用BULK INSERT
而不是UPDATE
。
你可以尝试这样做:
将数据批量插入临时表:
CREATE TABLE #temp (column_1 类型, column_2 类型, ...);
BULK INSERT #temp FROM 'C:\\FileName.csv' WITH (FIRSTROW = 2, FIELDTERMINATOR = ',', ROWTERMINATOR = '\n');
使用匹配列上的JOIN
更新目标表:
UPDATE t SET t.column_1 = temp.column_1, t.column_2 = temp.column_2 FROM 表 t JOIN #temp ON t.id = temp.id;
此外,如果您有任何权限问题,您应该与数据库管理员联系以提供您所需的权限。
英文:
I prefer myself to use the BULK INSERT
instead of UPDATE
.
Could you try this?
Bulk insert data into a temp table:
CREATE TABLE #temp (column_1 type, column_2 type, ...);
BULK INSERT #temp FROM 'C:\\FileName.csv' WITH (FIRSTROW = 2, FIELDTERMINATOR = ',', ROWTERMINATOR = '\n');
Update the target table using a JOIN
on matching columns:
UPDATE t SET t.column_1 = temp.column_1, t.column_2 = temp.column_2 FROM table t JOIN #temp ON t.id = temp.id;
Also, if you have any permission issues, you should check with the database administrator to provide you with the necessary permissions.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论