SQL Server CREATE Trigger

Source: Internet
Author: User
Tags getdate

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

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.