插入更新触发器如何判断是插入还是更新

Insert Update trigger how to determine if insert or update(插入更新触发器如何判断是插入还是更新)
本文介绍了插入更新触发器如何判断是插入还是更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在表 A 上编写一个插入、更新触发器,它将删除表 B 中的所有行,这些行的一列(比如 Desc)的值类似于在表 A 的列(比如 Col1)中插入/更新的值.我将如何编写它以便我可以处理更新和插入情况.我如何确定触发器是为更新还是插入执行的.

I need to write an Insert, Update Trigger on table A which will delete all rows from table B whose one column (say Desc) has values like the value inserted/updated in the table A's column (say Col1). How would I go around writing it so that I can handle both Update and Insert cases. How would I determine if the trigger is executed for an update or insert.

推荐答案

触发器有特殊的 INSERTEDDELETED 表来跟踪之前"和之后"数据.所以你可以使用类似 IF EXISTS (SELECT * FROM DELETED) 的东西来检测更新.更新时您只有 DELETED 中的行,但 INSERTED 中总是有行.

Triggers have special INSERTED and DELETED tables to track "before" and "after" data. So you can use something like IF EXISTS (SELECT * FROM DELETED) to detect an update. You only have rows in DELETED on update, but there are always rows in INSERTED.

寻找插入"在创建触发器中.

编辑,2011 年 11 月 23 日

Edit, 23 Nov 2011

评论后,此答案仅适用于 INSERTEDUPDATED 触发器.
显然,DELETE 触发器不能有INSERTED 中的总是行";正如我上面所说的

After comment, this answer is only for INSERTED and UPDATED triggers.
Obviously, DELETE triggers can not have "always rows in INSERTED" as I said above

这篇关于插入更新触发器如何判断是插入还是更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)