我也遇到了这个问题,并且能够解决它。这是我的情况:
就我而言,我有一个数据库用于报告分析数据(MYTARGET_DB),该数据库是从源系统(MYSOURCE_DB)提取的。一些“ MYTARGET_DB”表对于该系统是唯一的,并且数据是在该系统中创建和管理的。大多数表来自“ MYSOURCE_DB”,并且有一个作业可以将数据从“ MYSOURCE_DB”中删除/插入到“ MYTARGET_DB”中。
查找表之一[PRODUCT]来自SOURCE,并且在TARGET中存储了一个数据表[InventoryOutsourced]。表中设计了参照完整性。因此,当我尝试运行删除/插入时,会收到此消息。
Msg 50000, Level 16, State 1, Procedure uspJobInsertAllTables_AM, Line 249
The DELETE statement conflicted with the REFERENCE constraint "FK_InventoryOutsourced_Product". The conflict occurred in database "ProductionPlanning", table "dbo.InventoryOutsourced", column 'ProdCode'.
我创建的解决方法是将数据从[InventoryOutsourced]插入[@tempTable]表变量,删除[InventoryOutsourced]中的数据,运行同步作业,然后从[@tempTable]插入[InventoryOutsourced]。这样可以保持完整性,并保留唯一的数据收集。两全其美。希望这可以帮助。
BEGIN TRY
BEGIN TRANSACTION InsertAllTables_AM
DECLARE
@BatchRunTime datetime = getdate(),
@InsertBatchId bigint
select @InsertBatchId = max(IsNull(batchid,0)) + 1 from JobRunStatistic
--<DataCaptureTmp/> Capture the data tables unique to this database, before deleting source system reference tables
--[InventoryOutsourced]
DECLARE @tmpInventoryOutsourced as table (
[ProdCode] VARCHAR (12) NOT NULL,
[WhseCode] VARCHAR (4) NOT NULL,
[Cases] NUMERIC (8) NOT NULL,
[Weight] NUMERIC (10, 2) NOT NULL,
[Date] DATE NOT NULL,
[SourcedFrom] NVARCHAR(50) NOT NULL,
[User] NCHAR(50) NOT NULL,
[ModifiedDatetime] DATETIME NOT NULL
)
INSERT INTO @tmpInventoryOutsourced (
[ProdCode]
,[WhseCode]
,[Cases]
,[Weight]
,[Date]
,[SourcedFrom]
,[User]
,[ModifiedDatetime]
)
SELECT
[ProdCode]
,[WhseCode]
,[Cases]
,[Weight]
,[Date]
,[SourcedFrom]
,[User]
,[ModifiedDatetime]
FROM [dbo].[InventoryOutsourced]
DELETE FROM [InventoryOutsourced]
--</DataCaptureTmp>
... Delete Processes
... Delete Processes
--<DataCaptureInsert/> Capture the data tables unique to this database, before deleting source system reference tables
--[InventoryOutsourced]
INSERT INTO [dbo].[InventoryOutsourced] (
[ProdCode]
,[WhseCode]
,[Cases]
,[Weight]
,[Date]
,[SourcedFrom]
,[User]
,[ModifiedDatetime]
)
SELECT
[ProdCode]
,[WhseCode]
,[Cases]
,[Weight]
,[Date]
,[SourcedFrom]
,[User]
,[ModifiedDatetime]
FROM @tmpInventoryOutsourced
--</DataCaptureInsert>
COMMIT TRANSACTION InsertAllTables_AM
END TRY
INVENTORY_ITEMS
这两个语句之间添加了新的语句DELETE
。