如何获取触发器中已删除测试的值?

发布于 2024-09-25 10:52:47 字数 375 浏览 0 评论 0原文

我需要在 Table1 上的“删除”上放置一个触发器。从 Table1 中删除记录时,我需要在触发器中更新 Table2,但我需要触发器中已删除记录的值。 示例:-

IF OBJECT_ID ('UpdateLog','TR') IS NOT NULL
    DROP TRIGGER UpdateLog;
GO
CREATE TRIGGER UpdateLog
ON Table_1
AFTER DELETE 
AS
   UPDATE Table_2
    SET Date1 = getdate()
    WHERE (UID from deleted record from Table1)
GO

所以我需要从表 1 中删除的记录的值来更新表 2。如何?

I need to put a trigger on Delete on Table1. On delete of record from Table1, I need to update Table2 in trigger, but i need the value of deleted record in the trigger.
Example:-

IF OBJECT_ID ('UpdateLog','TR') IS NOT NULL
    DROP TRIGGER UpdateLog;
GO
CREATE TRIGGER UpdateLog
ON Table_1
AFTER DELETE 
AS
   UPDATE Table_2
    SET Date1 = getdate()
    WHERE (UID from deleted record from Table1)
GO

So I need value of deleted record from table1 to update the table2. How?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澉约 2024-10-02 10:52:47

它应该位于触发器中可用的“已删除”表中。请参阅使用插入和删除的表

select * from deleted

请注意,如果您运行删除多条记录可以被删除,并且您的触发器应考虑到“已删除”表包含不止一行。

大致如下:

UPDATE t
from table_2 t
inner join deleted d on d.UID = t.UID
    SET t.Date1 = getdate()

It should be in the 'deleted' table available in the trigger. See using the inserted and deleted tables

select * from deleted

Note that if you run delete mutiple records can be deleted and your trigger should take into account that the 'deleted' table contains more than one row.

Something along the lines of :

UPDATE t
from table_2 t
inner join deleted d on d.UID = t.UID
    SET t.Date1 = getdate()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文