我想做一个 sql 触发器 该怎么写?

来源:百度知道 编辑:UC知道 时间:2024/05/22 11:01:59
我是SQL 初学者,想写一个 sql 触发器,不知道该怎么写?请高手帮忙!
我有两个表 A ,B,
我想在B表中增加行的时候,更新A表。
例如:
A表中有列,NO1,NO2,NO3,
B表中有列,NO1,a,b,c,
在增加B表的行是时,如果B中的b列为“建立”或“恢复”时,
更新A表中的NO2列为“在职”,
在增加B表的行是时,如果B中的b列为“中断”或‘转移’时,
更新A表中的NO2列为“停保”,
在增加B表的行是时,如果B中的b列为“退休”时,
更新A表中的NO2列为“退休”,

if Exists(select name from sysobjects where type='tr' and name='Tr1')
drop trigger Tr1
go
create trigger Tr1 on B for insert
as begin
Declare @NO1 int,@b char
select @NO1=NO1 from inserted
select @b=b from inserted
if (@b='建立'or @b='恢复')
update A set NO2='在职' where NO1=@NO1

if (@b='中断'or @b='转移')
update A set NO2='停保' where NO1=@NO1

if (@b='退休')
update A set NO2='退休' where NO1=@NO1

end

CREATE TRIGGER trigger_name
ON { table | view }
[ WITH ENCRYPTION ]
{
{ { FOR | AFTER | INSTEAD OF } { [ INSERT ] [ , ] [ UPDATE ] }
[ WITH APPEND ]
[ NOT FOR REPLICATION ]
AS
[ { IF UPDATE ( column )
[ { AND | OR } UPDATE ( column ) ]
[ ...n ]
| IF ( COLUMNS_UPDATED ( ) { bitwise_operator } updated_bit