Capture changed rows in the SQL update trigger

How can catch the modified row(s) in update trigger to insert them into other table ?

In SQL Server we have only two virtual tables you can workwith inside a trigger, those are “INSERTED” and “DELETED”. For example:

Insert Operation:When you insert a new record “INSERTED” virtual table contains the newly inserted record, where as “DELETED” virtual remains empty.

Update Operation:When you update any record, first the old record will be placed into the “DELETED” virtual table and the newly updated record is hold by the “INSERTED” virtual table.

That means you can get the old value from “DELETED” and the currently updating value through “INSERTED” virtual table. you can query them like:

-- To get the old record value

SELECT * FROM DELETED

-- To get the updated value

SELECT * FROM INSERTED

Delete Operation:When you delete any particular record the deleted record will be inserted into the “DELETED” virtual table.

  1. SELECT * FROM UPDATED
    – gives ERROR.

  2. Try this:

    DECLARE @OldVal int, @NewVal int

    SELECT @OldVal = Col FROM DELETED

    SELECT @NewVal = Col FROM INSERTED

by holding the old and new values you can compare their state.

Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 后端存储 » Capture changed rows in the SQL update trigger

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录