Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

SQL Server create trigger

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

-- TABLE INSERTVAL UPDATEVALif (object_id('DATA_SYNC_FH_DJ','TR') is not null) drop trigger DATA_SYNC_FH_DJgocreate trigger DATA_SYNC_FH_DJon FH_DJ for insert,update,deleteas declare @oldUpdate varchar(20), @newDate varchar(20), @DJdanhao varchar(20), @Djid int, @isInsert bit, @isUpdate bit, @isDelete bit; --Determine whether it is an insertion operation IF EXISTS (SELECT 1 FROM inserted) AND NOT EXISTS (SELECT 1 FROM deleted)BEGINSET @isInsert = 1;select @Djid = djid from inserted;ENDELSESET @isInsert = 0--Determine if update operation IF EXISTS (SELECT 1 FROM inserted) AND EXISTS (SELECT 1 FROM deleted)BEGINSET @isUpdate = 1;select @Djid = djid from inserted; ENDELSESET @isUpdate = 0--Determine if delete operation IF (NOT EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted))BEGINSET @isDelete = 1;select @DJdanhao = DJdanhao from deleted;ENDELSESET @isDelete = 0 --Data before update select @oldUpdate = F_SYNC_UPDATE from deleted; --When modified by application, F_SYNC_UPDATE=null or F_SYNC_UPDATE=0. In this case, F_SYNC_DATE timestamp does not need to be updated, and records are not required to delete records. if ((@oldUpdate is null) or (@oldUpdate = 0)) begin --Update operation, update timestamp F_SYNC_DATE= systestamp and F_SYNC_UPDATE=null if (@isUpdate = 1)insert into DATA_SYNC_B_OPERATOR (t_name, o_type, o_date, VKEYS)values ('FH_DJ', 2, GETDATE(), @Djid);--Insert the newly added record into the operation record table if (@isInsert = 1) insert into DATA_SYNC_B_OPERATOR (t_name, o_type, o_date, VKEYS) values ('FH_DJ', 1, GETDATE(), @Djid);--Add the primary key of the deleted record to the operation record table if (@isDelete = 1) insert into DATA_SYNC_B_OPERATOR (t_name, o_type, o_date, VKEYS) values ('FH_DJ', 3, GETDATE(), 'test@' + @DJdanhao); endgo

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report