触发器deleted 表和 inserted 表详解!!!
2009-05-23 10:05 苏飞 阅读(13103) 评论(3) 编辑 收藏 举报阅读全文并下载代码:http://www.sufeinet.com/thread-264-1-1.html
create trigger updateDeleteTime
on user
for update
as
begin
update user set UpdateTime=(getdate()) from user inner join inserted on user.UID=Inserted.UID
end
on user
for update
as
begin
update user set UpdateTime=(getdate()) from user inner join inserted on user.UID=Inserted.UID
end
上面的例子是在执行更新操作的时候同时更新,一下修改时间。
关键在于Inserted表
触发器语句中使用了两种特殊的表:deleted 表和 inserted 表。
Deleted 表用于存储 DELETE 和 UPDATE 语句所影响的行的复本。在执行 DELETE 或 UPDATE 语句时,行从触发器表中删除,并传输到 deleted 表中。Deleted 表和触发器表通常没有相同的行。
Inserted 表用于存储 INSERT 和 UPDATE 语句所影响的行的副本。在一个插入或更新事务处理中,新建行被同时添加到 inserted 表和触发器表中。Inserted 表中的行是触发器表中新行的副本。
1.插入操作(Insert)
Inserted表有数据,Deleted表无数据
2.删除操作(Delete)
Inserted表无数据,Deleted表有数据
3.更新操作(Update)
Inserted表有数据(新数据),Deleted表有数据(旧数据)
应用实例

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,sufei>
-- Create date: <Create Date,2010-05-11>
-- Description: <当是短信充值时修改相信的记录使记录不会重复获取>
-- =============================================
ALTER TRIGGER [dbo].[updatestart]
ON [dbo].[OrderTelecom] FOR update
AS
BEGIN
DECLARE @state int;
DECLARE @note2 varchar(50)
SELECT @state= Inserted.ortState,@note2 =Inserted.ortNote2 from Inserted
IF @state=1 AND @note2=1
begin
--当发短信猫取走记录时修改状态为成功和取过的状态
update OrderTelecom set OrderTelecom.ortState=2 ,OrderTelecom.ortSmsmessages='短信充值成功'
from OrderTelecom inner join Inserted on OrderTelecom.ortId=Inserted.ortId
end
if @state in(2,3,10) and @note2=0
begin
update OrderTelecom set ortNote2=1
from OrderTelecom inner join Inserted on OrderTelecom.ortId=Inserted.ortId
end
END
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,sufei>
-- Create date: <Create Date,2010-05-11>
-- Description: <当是短信充值时修改相信的记录使记录不会重复获取>
-- =============================================
ALTER TRIGGER [dbo].[updatestart]
ON [dbo].[OrderTelecom] FOR update
AS
BEGIN
DECLARE @state int;
DECLARE @note2 varchar(50)
SELECT @state= Inserted.ortState,@note2 =Inserted.ortNote2 from Inserted
IF @state=1 AND @note2=1
begin
--当发短信猫取走记录时修改状态为成功和取过的状态
update OrderTelecom set OrderTelecom.ortState=2 ,OrderTelecom.ortSmsmessages='短信充值成功'
from OrderTelecom inner join Inserted on OrderTelecom.ortId=Inserted.ortId
end
if @state in(2,3,10) and @note2=0
begin
update OrderTelecom set ortNote2=1
from OrderTelecom inner join Inserted on OrderTelecom.ortId=Inserted.ortId
end
END
本人的博客不再维护从2013年就不再维护了
需要我帮助的朋友请到我的个人论坛 http://www.sufeinet.com 进行讨论,感谢大家对我的支持!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述