Self-referencing constraint in MS SQL

前端 未结 3 1838
半阙折子戏
半阙折子戏 2020-12-11 15:58

Is it true that MS SQL restrict self-referencing constraints with ON DELETE CASCADE option? I have a table with parent-child relation, PARENT_ID column is foreign key for I

3条回答
  •  死守一世寂寞
    2020-12-11 16:17

    CREATE TRIGGER MyTable_OnDelete ON MyTable
    INSTEAD OF DELETE
    AS 
    BEGIN
    
      SET NOCOUNT ON;
    
      DELETE FROM mt
      FROM   deleted AS D
      JOIN   MyTable AS mt
      ON     d.Id = mt.ParentId
    
      DELETE FROM mt
      FROM   deleted AS D
      JOIN   MyTable AS mt
      ON     d.Id = mt.Id
    
    END
    

提交回复
热议问题