0

Problem: I am trying to insert a .csv file with thousands/millions of rows into a SQL Server table with a bulk insert query. Now I want to update the table with bulk update where specific column is change e.g where price is changed. How can I do this? And also: I want to ignore constraint when inserting into the table

BULK INSERT Table
FROM 'D:\test.csv'
WITH
    (FIELDTERMINATOR = ',',
     ROWTERMINATOR = '\n')
GO

Now e.g table contains price column when second time I update the file only that row update which has different price before

4

2 回答 2

3

分两个(或更多)步骤进行。

插入原始数据(缺陷和所有),然后运行单独的更新语句以使其看起来像您想要的那样。

对于大型或繁忙的表,或者为了在准备好之前将新数据保持分离,您可能还需要先批量插入到单独的保持表中,在那里对数据进行按摩和清理,然后从保持表迁移到决赛桌。

或者,您可以编写一个客户端程序在批量插入之前预先清理数据,或者您可以使用 Sql Server Integration Services (SSIS) 之类的工具来处理导入。SSIS 有很多很好的特性来处理这种事情。

您无法做的是对批量插入代码进行简单或快速的调整。它做它所做的,仅此而已。

于 2017-02-22T16:57:38.247 回答
1

您不能只在前一次上传的基础上批量上传一个文件,而只记录差异。您可以完全刷新数据(即:覆盖的完整加载),将完整的 CSV 上传到临时表并使用 SQL 代码比较两个表,或者您可以使用 SSIS 等工具连接到 CSV 文件,检查它针对表中的 vales 并从那里触发更新。

于 2017-02-22T16:57:57.500 回答