MERGE与FOREIGN KEY约束发生冲突

时间:2017-07-22 13:53:34

标签: sql sql-server merge insert

我有这个更新文件,我将新数据加载到我的数据库或更新现有数据。但我得到了这些错误。

The MERGE statement conflicted with the FOREIGN KEY constraint
"FK_JW_MaterialData2PL_cMat_MaterialText". 
 The conflict occurred in database "TEST", table "dbo.JW_MaterialData", column 'camosGUID'.

The MERGE statement conflicted with the FOREIGN KEY constraint
"FK_JW_MaterialData_cMat_Material". The conflict occurred in 
database "TEST", table "dbo.cMat_Material", column 'camosGUID'.

这是相关的陈述。但是当然还有更多的MERGE语句和更多表格。

MERGE INTO [dbo].[JW_MaterialData2PL] ON camosGUID
WHEN MATCHED THEN UPDATE SET 
WHEN NOT MATCHED THEN INSERT 

MERGE INTO [dbo].[cMat_Material] ON camosGUID
WHEN MATCHED THEN UPDATE SET 
WHEN NOT MATCHED THEN INSERT 

MERGE INTO [dbo].[JW_MaterialData] ON camosGUID
WHEN MATCHED THEN UPDATE SET 
WHEN NOT MATCHED THEN INSERT 

FK_JW_MaterialData2PL_cMat_MaterialTextFK_JW_MaterialData_cMat_Material设置为ON UPDATE CASCADEON DELETE CASCADE

这可能是我放置MERGE语句的顺序问题?或者你知道可能是什么原因吗?

另一个问题:当我再次运行脚本时,没有错误。那么只运行scipt两次还是会导致将来出现问题?

1 个答案:

答案 0 :(得分:0)

有必要通过查看表的依赖关系以正确的顺序放置MERGE语句。

  1. cMat_Material
  2. JW_MaterialData
  3. JW_MaterialData2PL