sql 触发器使用例子

inserted,deleted是在触发器中使用的两个临时表,当执行insert操作时,在inserted中存储着当前插入的记录,在执行delete操作时,在deleted中存储着当前删除的记录,当执行update时,在inserted中存储着修改后的记录,在deleted中存储着修改前的记录。


复制代码 代码如下:

set ansi_nulls on

set quoted_identifier on

go

alter trigger [dnt_user_add]

on [dbo].[dnt_users]

after insert

as

begin

set nocount on;

insert into [dnt_userfields](uid)

select uid from inserted

end


删除:


复制代码 代码如下:

set ansi_nulls on

go

set quoted_identifier on

go

create trigger [dnt_users_del]

on [dnt_users]

after delete

as

begin

set nocount on;

delete [dnt_userfields] from deleted where [dnt_userfields].uid=deleted.uid

end

go

修改:


复制代码 代码如下:

set ansi_nulls on

go

set quoted_identifier on

go

alter trigger [dnt_users_up]

on [dnt_users]

after update

as

begin

set nocount on;

update [dnt_userfields] set icq = inserted.uid from inserted where [dnt_userfields].uid = inserted.uid

end

go

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐