如何判断是插入还是更新

Posted

技术标签:

【中文标题】如何判断是插入还是更新【英文标题】: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 语句可以更快。然后让另一个进程提取额外表中的行并相应地处理它们。 【参考方案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。

以上是关于如何判断是插入还是更新的主要内容,如果未能解决你的问题,请参考以下文章

SQL在更新数据时,先判断数据是不是存在。存在就更新update,不存在就插入insert。

JAVA里面如何判断插入/删除/更新数据库是不是成功

mysql 先判断表中的某一字段 如果为空 插入数据 否则 更新该条数据

(十万火急在线等)php mysql中数据插入A表时同步更新B表中数据用php如何实现?

mybatis想给其中某个字段赋成null,mapper层怎么判断这个传进来的null是 需要赋空值还是需要略过不更新

python 判断更新插入数据库