SQL Server Trigger switching Insert,Delete,Update

后端 未结 5 1904
慢半拍i
慢半拍i 2021-02-09 01:39

Hello is possible to switch between DML commands/operations (Insert,Delete,Update) on Trigger Body?, I try to snippet some T-SQL for understand me better :

CREA         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-09 02:15

    You can use one trigger for all commands/operations by use union join;

    CREATE TRIGGER DML_ON_TABLEA
     ON  TABLEA
    AFTER INSERT,DELETE,UPDATE
    AS 
    BEGIN
        SET NOCOUNT ON;
       --logic for insert
        insert into Backup_table (columns_name) select columns_name from inserted i
        --logic for delete
       UNION ALL
        insert into Backup_table (columns_name) select columns_name from deleted d
    END
    GO
    

    --note update command like inserted command but have another command deleted

提交回复
热议问题