SQL Server: Self-reference FK, trigger instead of ON DELETE CASCADE

后端 未结 1 1219
滥情空心
滥情空心 2020-12-07 02:41

I need to perform an ON DELETE CASCADE on my table named CATEGORY, which has the following columls CAT_ID (BIGINT) NAME (VARCHAR) PARENT_CAT_ID (BIGINT)

PARENT_CAT_I

相关标签:
1条回答
  • 2020-12-07 03:36

    The FOR DELETE trigger is raised after the original DELETE has been executed. To delete recursively, you need to write an INSTEAD OF DELETE trigger.

    The algorithm is like this:

    • Insert the PKs from deleted into a temp table

    • Find detail records of records in temp table

    • Loop until no more records are found

    DELETE records in the table by joining with temp table.

    I described recursive deletion in my blog.

    Update

    I guess you just need to drop that ON DELETE CASCADE flag from your recursive foreign key in Categories. The CASCADE flag on the foreign key from CAT_SCH should not matter.

    0 讨论(0)
提交回复
热议问题