SQL Server Trigger Insert,delete,update

Source: Internet
Author: User
Tags getdate

-- =============================================--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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.