【问题标题】:How to determine if insert or update如何判断是插入还是更新
【发布时间】:2014-09-25 16:03:12
【问题描述】:

每当在 CUSTOMER 表中发生 INSERT 时,我需要调用“StoredProcedure1”和 更新发生在客户表中,我需要在触发器中调用“StoredProcedure2”。 如何确定是在触发器中插入还是更新来自 SQL Server 2008。

有人可以帮我解决一下吗?

代码:

CREATE TRIGGER Notifications ON CUSTOMER
FOR INSERT,UPDATE
AS
BEGIN
DECLARE @recordId varchar(20);
set @recordId= new.Id;
    //if trigger is insert at the time I call to SP1
        EXEC StoredProcedure1 @recordId
    //if trigger is Upadeted at the time I call to SP2
        EXEC StoredProcedure2 @recordId
END

【问题讨论】:

  • 不建议使用一次必须执行一行的触发调用程序。真的很伤演技。为什么不将 SP 逻辑移到触发器中??
  • 这里有几件真正重要的事情。首先,您永远不应该在从插入或删除的值填充的触发器中使用标量变量。 sql server 中的触发器每次操作触发一次,您的触发器需要处理多行操作。其次,如果您需要与更新不同的插入逻辑,请创建两个触发器。一种用于每种类型的操作。
  • 我需要给客户发邮件
  • 不要在触发器中发送电子邮件。您会将系统速度降低到令人难以置信的低水平。在这种情况下,您应该做的是使用触发器来填充队列表,以便 dml 语句可以更快。然后让另一个进程提取额外表中的行并相应地处理它们。

标签: sql-server triggers


【解决方案1】:

让 SQL Server 成为 SQL Server,让它为您工作!

为每个更改事件(插入、更新和/或删除)创建单独的触发器。 将每个逻辑放入需要它的触发器中。 无需检查事件类型。

并且不要调用一个过程,除非它快速、快速并且不能阻塞其他人。

【讨论】:

  • 好的,我从不打电话给 Stored Procure,我可以在 Trigger 中向客户发送电子邮件吗?
  • sp_send_dbmail 是调用它的 msft 系统过程。当您尝试使用 SQL Server 通过电子邮件向内部用户发送错误消息以外的任何内容时,您可能会受到一团糟的伤害。我会将消息请求排队到一个表中,并安排一个作业定期发送电子邮件。
【解决方案2】:

试试这个代码来触发 INSERT、UPDATE 和 DELETE。这在 Microsoft SQL SERVER 2008 上运行良好

if (Select Count(*) From inserted) > 0 and (Select Count(*) From deleted) = 0
begin
   print ('Insert...')
end

if (Select Count(*) From inserted) = 0 and (Select Count(*) From deleted) > 0
begin
   print ('Delete...')
end

if (Select Count(*) From inserted) > 0 and (Select Count(*) From deleted) > 0
begin
   print ('Update...')
end

【讨论】:

    【解决方案3】:

    解决此问题的最简单方法是使用两个触发器,一个用于插入,一个用于更新。

    CREATE TRIGGER InsertNotifications ON CUSTOMER
    FOR INSERT
    AS
    BEGIN
    DECLARE @recordId varchar(20);
    set @recordId= new.Id;
        //if trigger is insert at the time I call to SP1
            EXEC StoredProcedure1 @recordId
    
    END
    
    CREATE TRIGGER UpdateNotifications ON CUSTOMER
    FOR UPDATE
    AS
    BEGIN
    DECLARE @recordId varchar(20);
    set @recordId= new.Id;
        //if trigger is Upadeted at the time I call to SP2
            EXEC StoredProcedure2 @recordId
    END
    

    【讨论】:

      【解决方案4】:

      在 INSERT 中,虚拟 DELETED 表将为空。

      【讨论】:

      • 请告诉我如何实现我的方案,在 UPDATE 部分我需要获取所有旧值以与新值进行比较,然后最后我需要调用存储过程。
      【解决方案5】:
      create or replace trigger comp
      before
      insert or delete or update
      on student
      referencing old as o new as n
      for each row
      begin
        if deleting then
                 insert into student_backup values
            (:o.studid,:o.studentname,:o.address,:o.contact_no,:o.branch,sysdate);
        end if;
        if inserting then
              insert into student_backup values
            (:n.studid,:n.studentname,:n.address,:n.contact_no,:n.branch,sysdate);
       end if;
        if updating then
             insert into student_backup values
            (:o.studid,:o.studentname,:o.address,:o.contact_no,:o.branch,sysdate);
        end if;
      end comp;
      

      【讨论】:

      • 那是 Oracle 语法。 OP 询问了 SQL Server。
      猜你喜欢
      • 2014-03-11
      • 2012-09-24
      • 1970-01-01
      • 2010-10-18
      • 1970-01-01
      • 2016-09-16
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多