-- 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
新闻标题:SQLServer创建触发器
当前网址:https://www.cdcxhl.com/article20/gsepco.html
成都网站建设公司_创新互联,为您提供企业网站制作、服务器托管、搜索引擎优化、、网页设计公司、面包屑导航
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联