SQL create trigger

Source: Internet
Author: User

SQL create trigger syntax

SQL create trigger

Trigger creation syntax

Create trigger triggername on tablename
For insert | delete | update
As
Begin

End

Let's take a look at the instance for creating a trigger.

Create table ta1
(
Taid int identity (1001,1) primary key,
Taname varchar (20) not null,
Tasl int
)


Create table ta2
(
Ta2id int identity (1001,1) primary key,
Ta2name varchar (20) not null,
Ta2ysh int,
Ta2sl int
)


Create trigger tru_ta1
On ta1
For update
As
Begin
If exists (select 1 from ta2, inserted where ta2id = taid and ta2ysh <tasl)
Rollback
Else
Update ta2 set ta2sl = tasl
From inserted where ta2id = taid
End

The above functions are examples

The request is triggered when I modify the tasl of ta1. First, compare whether the tasl IN THE ta1 table is greater than the ta2ysh IN THE ta2 table. If the tasl IN THE ta2 table is greater than the ta2ysh IN THE ta2 table, the modification is not allowed, otherwise, modify the ta2sl in the ta2 table and then the tasl in the ta1 table.

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.