简体   繁体   English

SQL 服务器覆盖违反唯一约束

[英]SQL Server overwrite unique constraint violation

I have two files which I am importing via Node JS to SQL Server.我有两个文件,我通过 Node JS 导入到 SQL 服务器。 The table has unique key for equity instrument identifier (ISIN)该表具有权益工具标识符 (ISIN) 的唯一键

data1.csv and data2.csv data1.csvdata2.csv

I first import data1.csv each row is inserted to the database.我先导入data1.csv,每一行都插入到数据库中。 After this I import data2.csv (the values are again inserted to database) which may contain the same ISIN, but it's related values are higher priority than the first file (there are not many of these ISINs 5 out of 1000 or so).在此之后,我导入 data2.csv(值再次插入数据库),它可能包含相同的 ISIN,但它的相关值比第一个文件具有更高的优先级(这些 ISIN 中的 5 个不多,大约 1000 个)。

What can I do with SQL server to overwrite the values if the unique constraint is violated?如果违反唯一约束,我可以用 SQL 服务器做什么来覆盖值? I understand that there is an option to upload data2.csv first, however there are some external constrains that do not allow me to do that.我知道有一个选项可以先上传 data2.csv,但是有一些外部限制不允许我这样做。

Please tell me if additional information is required请告诉我是否需要其他信息

I would recommend staging process to do this:我会推荐分期过程来做到这一点:
1. create a staging table with similar schema as your target table 1. 创建一个与目标表具有相似架构的临时表
2. Before loading delete all rows from staging table (you can use truncate) 2.加载前删除登台表中的所有行(你可以使用截断)
3. Upload the file to the staging table 3.上传文件到暂存表
4. Load your data into final table - here you can use some logic to only insert new rows and update existing rows. 4. 将数据加载到最终表中 - 在这里您可以使用一些逻辑来仅插入新行并更新现有行。 Merge command will be useful in scenario like this.合并命令在这种情况下很有用。

Repeat steps 2 to 4 for each source table.对每个源表重复步骤 2 到 4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM