欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

sql 触发器使用例子

程序员文章站 2023-12-04 17:50:05
inserted,deleted是在触发器中使用的两个临时表,当执行insert操作时,在inserted中存储着当前插入的记录,在执行delete操作时,在deleted...
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