当前位置: 数据库>sqlserver
sql 触发器使用例子
来源: 互联网 发布时间:2014-09-05
本文导语: inserted,deleted是在触发器中使用的两个临时表,当执行insert操作时,在inserted中存储着当前插入的记录,在执行delete操作时,在deleted中存储着当前删除的记录,当执行update时,在inserted中存储着修改后的记录,在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
代码如下:
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