zoukankan      html  css  js  c++  java
  • 触发器deleted 表和 inserted 表详解


    • 摘要:触发器语句中使用了两种特殊的表:deleted 表和 inserted 表。
    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

    上面的例子是在执行更新操作的时候同时更新,一下修改时间。
    关键在于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

                             

    文章来自:   http://sufei.cnblogs.com/   

  • 相关阅读:
    ZJOI2019二轮游记
    Luogu P5284 [十二省联考2019]字符串问题
    Luogu P5309 [Ynoi2012]D1T1
    Luogu P5292 [HNOI2019]校园旅行
    LOJ #6052. 「雅礼集训 2017 Day11」DIV
    Luogu P5279 [ZJOI2019]麻将
    LOJ #6060. 「2017 山东一轮集训 Day1 / SDWC2018 Day1」Set
    Luogu P5283 [十二省联考2019]异或粽子
    Luogu P5290 [十二省联考2019]春节十二响
    Luogu P5285 [十二省联考2019]骗分过样例
  • 原文地址:https://www.cnblogs.com/yzl495/p/4118776.html
Copyright © 2011-2022 走看看