如何确定是插入还是更新

编程入门 行业动态 更新时间:2024-10-26 15:25:48
本文介绍了如何确定是插入还是更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

每当CUSTOMER表中发生INSERT时,我都需要调用 StoredProcedure1 ,而CUSTOMER表中发生 UPDATE,则需要调用 StoredProcedure2 。 如何从SQL Server 2008中确定是在触发器中插入还是更新。

Whenever INSERT is happened in the CUSTOMER table,I need to call the "StoredProcedure1"and UPDATE is happend in the CUSTOMER table,I need to call the "StoredProcedure2" in the Trigger. How to determine if insert or update in the trigger from SQL Server 2008.

有些人可以帮助我如何解决?

Some one can please help me how to solve?

代码:

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

推荐答案

尝试使用此代码来触发插入,更新和删除。如果在Microsoft SQL SERVER 2008上正常运行,则可以正常工作。

Try this code for trigger for INSERT, UPDATE and DELETE. This works fine on 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

更多推荐

如何确定是插入还是更新

本文发布于:2023-11-24 10:04:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624771.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!