sql 触发器使用例子

前端之家收集整理的这篇文章主要介绍了sql 触发器使用例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

inserted,deleted是在触发器中使用的两个临时表,当执行insert操作时,在inserted中存储着当前插入的记录,在执行delete操作时,在deleted中存储着当前删除的记录,当执行update时,在inserted中存储着修改后的记录,在deleted中存储着修改前的记录。
<div class="codetitle"><a style="CURSOR: pointer" data="85441" class="copybut" id="copybut85441" onclick="doCopy('code85441')"> 代码如下:

<div class="codebody" id="code85441">
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
select uid from inserted END

删除


<div class="codetitle"><a style="CURSOR: pointer" data="77155" class="copybut" id="copybut77155" onclick="doCopy('code77155')"> 代码如下:
<div class="codebody" id="code77155">
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

修改


<div class="codetitle"><a style="CURSOR: pointer" data="55128" class="copybut" id="copybut55128" onclick="doCopy('code55128')"> 代码如下:
<div class="codebody" id="code55128">
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

原文链接:https://www.f2er.com/mssql/64350.html
sqlsql触发器触发器触发器

猜你在找的MsSQL相关文章