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.