插入更新触发器如何确定是插入还是更新 [英] Insert Update trigger how to determine if insert or update

查看:105
本文介绍了插入更新触发器如何确定是插入还是更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在表A上编写一个插入,更新触发器,该操作将删除表B的所有行,其中一行(例如Desc)的值类似于在表A的列中插入/更新的值(例如Col1)。我将如何编写它,以便同时处理Update和Insert案例。我将如何确定是否为更新或插入执行触发器。

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.

推荐答案

触发器具有特殊的 INSERTED DELETED 表来跟踪之前和之后。数据。因此,您可以使用 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

评论后,此答案仅适用于已插入 UPDATED 触发器。

显然,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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆