admin管理员组文章数量:1123223
I have a conditional update that I manually run from time to time. The conditions will always be on Loc and Item. The statement updates the [StockType] column for the rows that meet the conditions.
Ideally, when the manual update runs the trigger will fire and update additional columns (PreviousStockType and DateLastChanged) within that same table against the specific rows (that match the manual update condition)
The trigger, however, is updating ALL rows instead of just the rows specified in the manual update condition, what am I doing wrong?
Here's the script for the manual update:
Update Client_Manual_StockType
set StockType = 'Non-Stocked'
where
Loc = 'Loc-1' and item = '0005646'
and here's the trigger:
CREATE TRIGGER [dbo].[trg_UpdateStockType]
ON [dbo].[Client_Manual_StockType]
AFTER UPDATE
AS
BEGIN
-- Update PreviousStockType and DateLastChanged
UPDATE Client_Manual_StockType
SET
PreviousStockType = d.StockType,
DateLastChanged = GETDATE()
FROM inserted i
JOIN deleted d ON i.Loc = d.Loc
And i.Item = d.Item
WHERE i.StockType <> d.StockType
AND EXISTS (
SELECT 1
FROM inserted
WHERE Loc = i.Loc AND Item = i.Item
);
END
Table BEFORE running manual update:
Loc | Item | StockType | PreviousStockType | DateLastChanged |
---|---|---|---|---|
Loc-1 | 0005646 | Stocked | NULL | NULL |
Loc-1 | test1 | Stocked | NULL | NULL |
Loc-2 | test2 | Stocked | NULL | NULL |
Loc-2 | test3 | Stocked | NULL | NULL |
本文标签: sqlTrigger updating all instead of specific rowsStack Overflow
版权声明:本文标题:sql - Trigger updating all instead of specific rows - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736560181a1944634.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论