在SQL Server中,当创建外键时,如果出现多个级联路径,就会出现上述错误。 为了解决这个问题,可以通过禁用级联删除或使用触发器来解决。 下面是使用触发器的示例代码:
创建触发器:
CREATE TRIGGER trg_JobDetails_Delete_StokCards ON JobDetails INSTEAD OF DELETE AS BEGIN SET NOCOUNT ON;
--删除JobDetails中的行
DELETE FROM JobDetails WHERE JobDetailsID IN (SELECT JobDetailsID FROM deleted);
--删除StokCards中的行
DELETE FROM StokCards WHERE StokCardID IN (SELECT StokCardID FROM deleted);
END
然后,将外键的级联删除选项更改为NO ACTION:
ALTER TABLE JobDetails DROP CONSTRAINT FK_JobDetails_StokCards_StokCardID GO ALTER TABLE JobDetails ADD CONSTRAINT FK_JobDetails_StokCards_StokCardID FOREIGN KEY (StokCardID) REFERENCES StokCards(StokCardID) ON DELETE NO ACTION GO