-- 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, @newDate varchar (), @DJdanhao varchar, @Djid int, @ isinsert bit, @isUpdate bit, @isDelete bit; -- determine if the insert operation is if exists (select 1 from inserted) and not exists (select 1 from deleted) BEGINSET @ isinsert = 1;select @Djid = djid from inserted; endelseset @isInsert = 0-- judgment isNo for 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 if is a delete operation (Not exists (select 1 from Inserted) and exists (select 1 from deleted)) beginset @isDelete = 1; select @DJdanhao = DJdanhao from deleted; endelseset @isDelete = 0 --Pre-update data select @oldUpdate = f_sync_update from deleted; -- When modified by the application, F_sync_update=null or f_sync_update=0, do not need to update the f_sync_date timestamp at this time, do not need to record delete records if (@oldUpdate is null) or (@oldUpdate = 0) begin --Update operation, update timestamp f_sync_date=systimestamp 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 log 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 (), ' [email protected] ' + @DJdanhao); endgo
This article is from the "write-free" blog, please be sure to keep this source http://7156680.blog.51cto.com/7146680/1790838
SQL Server CREATE Trigger