SQL Server trigger instance

Source: Internet
Author: User
I have never written a SQL Server trigger before. After a day of hard work, I finally wrote it. Now I want to record my experiences. The syntax of the SQL Server trigger is not mentioned. It mainly records the main items. Each start has a Begin content end. if you do not understand this before, it is inaccurate to trigger the write condition many times. The following is an example: if (object_id (inser

I have never written a SQL Server trigger before. After a day of hard work, I finally wrote it. Now I want to record my experiences. The syntax of the SQL Server trigger is not mentioned. It mainly records the main items. Each start has a Begin content end. if you do not understand this before, it is inaccurate to trigger the write condition many times. The following is an example: if (object_id ('inser

I have never written a SQL Server trigger before. After a day of hard work, I finally wrote it. Now I want to record my experiences.

The syntax of the SQL Server trigger is not mentioned. It mainly records the main items. Each start has a Begin content end. If you do not understand this before, it is inaccurate to trigger the write condition many times. The following is an example:

If (object_id ('insert _ black_list ', 'tr') is not null) drop trigger insert_black_list
Go
Create TRIGGER insert_black_list
On User_Infor_Message
After insert
As
Declare @ messagetype varchar (1)
Declare @ cardno varchar (10)
BEGIN
Select @ messagetype = messagetype, @ cardno = cardno from User_Infor_Message
If @ messagetype = '1' or @ messagetype = '5'
Begin
If (select count (cardno) from Balck_List where cardno = @ cardno) = 0
Begin
Insert into Balck_List select Inserted. cardno, Inserted. cardid, convert (varchar (8), getdate (), 112) from Inserted
End
End
Else if @ messagetype = '6'
Begin
Delete from Balck_List where cardno = @ cardno
End
END
Go

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.