看来,至少通过Management Studio Express,不可能设置引用同一表的外键约束来对更新或删除执行任何操作。
我有一个表,如果要删除一行,我想将更新级联为null。

这可能吗?

谢谢,

最佳答案

您需要使用INSTEAD OF DELETE触发器来处理这种情况。

就像是:

CREATE TRIGGER tr_IOD_YourTable ON YourTable
INSTEAD OF DELETE
AS
BEGIN
    SET NOCOUNT ON

    UPDATE yt
        SET ChildForeignKey = NULL
        FROM deleted d
            INNER JOIN YourTable yt
                ON d.PrimaryKeyColumn = yt.ChildForeignKey

    DELETE FROM yt
        FROM deleted d
            INNER JOIN YourTable yt
                ON d.PrimaryKeyColumn = yt.PrimaryKeyColumn
END

关于sql-server - SQL Server:是否可以级联更新自引用表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4347903/

10-10 22:37