您好,登錄后才能下訂單哦!
-- TABLE INSERTVAL UPDATEVAL if (object_id('DATA_SYNC_FH_DJ','TR') is not null) drop trigger DATA_SYNC_FH_DJ go create trigger DATA_SYNC_FH_DJ on FH_DJ for insert,update,delete as declare @oldUpdate varchar(20), @newDate varchar(20), @DJdanhao varchar(20), @Djid int, @isInsert bit, @isUpdate bit, @isDelete bit; -- 判斷是否為插入操作 IF EXISTS(SELECT 1 FROM inserted) AND NOT EXISTS(SELECT 1 FROM deleted) BEGIN SET @isInsert = 1; select @Djid = djid from inserted; END ELSE SET @isInsert = 0 -- 判斷是否為更新操作 IF EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted) BEGIN SET @isUpdate = 1; select @Djid = djid from inserted; END ELSE SET @isUpdate = 0 -- 判斷是否為刪除操作 IF (NOT EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)) BEGIN SET @isDelete = 1; select @DJdanhao = DJdanhao from deleted; END ELSE SET @isDelete = 0 --更新前的數據 select @oldUpdate = F_SYNC_UPDATE from deleted; --通過應用程序修改時,F_SYNC_UPDATE=null或F_SYNC_UPDATE=0,此時不需要更新F_SYNC_DATE 時間戳,也不需要記錄刪除記錄 if ((@oldUpdate is null) or (@oldUpdate = 0)) begin --更新操作,更新時間戳F_SYNC_DATE=systimestamp和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); --把新增加的記錄插入到操作記錄表 if (@isInsert = 1) insert into DATA_SYNC_B_OPERATOR (t_name, o_type, o_date, VKEYS) values ('FH_DJ', 1, GETDATE(), @Djid); --把刪除記錄的主鍵添加到操作記錄表 if (@isDelete = 1) insert into DATA_SYNC_B_OPERATOR (t_name, o_type, o_date, VKEYS) values ('FH_DJ', 3, GETDATE(), 'test@' + @DJdanhao); end go
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。