-- =============================================--Author: Wang Wei--Create date:2017-07-06--Description: Synchronize data to attendance schedule when adding, deleting and changing the opening schedule-- =============================================CREATE TRIGGER [dbo].[Tr_lm_sys_kaifangshijian_idu] on [dbo].[Lm_sys_kaifangshijian] for INSERT,DELETE,UPDATE as BEGIN --New if(exists(Select 1 fromInserted and not exists(Select 1 fromdeleted)) begin INSERT intoLm_kq_kaoqinganpai (kaoqinganpaiid, Shiyanshiid, Kaishiriqi, Jieshuriqi, Xingqingbianhao, Kaishishijian, Jieshushijian, SystemType, Createon, creat EDate, Updateon, updatedate)SELECT CONVERT(VARCHAR( -), A.kaifangshijianid) asKaifangshijianid,CONVERT(VARCHAR( -), A.SHIYANSHIID) asshiyanshiid, A.kaishiriqi, A.jieshuriqi, A.xingqingbianhao, A.kaishishijian, A.jieshushijian, 1 asSystemType,NULL , GETDATE() , NULL , GETDATE() fromInserted asAINNER JOINLm_sys_shiyanshixinxi asB onA.shiyanshiid=b.shiyanshiidINNER JOINLm_mj_men asC onB.menid=C.menidWHEREA.shifoumoren= 0 andB.isdelete= 0 andC.isdelete= 0; End --Delete if( not exists(Select 1 fromInserted and exists(Select 1 fromdeleted)) begin Delete fromLm_kq_kaoqinganpaiwhereKaoqinganpaiidinch(SelectKaifangshijianid fromdeleted)End --Update if(exists(Select 1 fromInserted and exists(Select 1 fromdeleted)) begin UpdateLm_kq_kaoqinganpaiSetShiyanshiid=A.kaifangshijianid, Kaishiriqi=A.kaishiriqi,jieshuriqi=A.jieshuriqi, Xingqingbianhao=A.xingqingbianhao,kaishishijian=A.kaishishijian, Jieshushijian=A.jieshushijian,updatedate=GETDATE() frominserted awhereKaoqinganpaiid=A.kaifangshijianidEnd ENDGO
SQL Server Trigger Insert,delete,update