How to determine if insert or update(如何判断是插入还是更新)
问题描述
每当在 CUSTOMER 表中发生 INSERT 时,我都需要调用StoredProcedure1"和更新发生在 CUSTOMER 表中,我需要在触发器中调用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.
有人可以帮我解决吗?
代码:
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
推荐答案
试试这个代码,用于 INSERT、UPDATE 和 DELETE 的触发器.这在 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
这篇关于如何判断是插入还是更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何判断是插入还是更新


- 在SQL中,如何为每个组选择前2行 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- SQL 临时表问题 2022-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01