SQL Server trigger application

Source: Internet
Author: User

I. Insert trigger ----------------

Create trigger tr_commissioninfo_insert on commission_info _
For insert
As
Begin
Declare @ commissionid varchar (20), @ date datetime
Set @ commissionid = (select ID _ from inserted)
Set @ date = getdate ()
Insert into cooperate_years _ (commission_id _, contract_start_date _, re_contract_start_date _) values (@ commissionid, @ date, @ date)
End

Ii. Update trigger

Create trigger tr_commissioninfo_update on commission_info _
For update
As
Begin
Declare @ commissionid varchar (20), @ old_comm_category int, @ new_comm_category int, @ date datetime
Set @ commissionid = (select ID _ from inserted)
Set @ old_comm_category = (select comm_cagegory _ from deleted)
Set @ new_comm_category = (select comm_cagegory _ from inserted)
Set @ date = getdate ()
If (@ old_comm_category <103 or @ old_comm_category> 106) and (@ new_comm_category> = 103 and @ new_comm_category <= 106 ))
Begin
If exists (select 1 from cooperate_years _ Where commission_id _ = @ commissionid)
Begin
Update cooperate_years _ set re_contract_start_date _ = @ date where commission_id _ = @ commissionid
End
Else
Begin
Insert into cooperate_years _ (commission_id _, contract_start_date _, re_contract_start_date _) values (@ commissionid, @ date, @ date)
End
End
End

 

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.