Trigger only modify updated/inserted row
Hi I have the following trigger.
ALTER TRIGGER [dbo].[DivisionLastModified] ON [dbo].[tb_Division]
WITH EXECUTE AS 开发者_开发百科CALLER
FOR INSERT, UPDATE
AS
BEGIN
UPDATE tb_Division SET LastModified = GetDate()
END
This updates all rows however I only wish to modify the update/added row. Is this achievable?
This is Because you update all Rows
UPDATE tb_Division SET LastModified = GetDate()
You must specify the last inserted or updated row id,
so you can specify that in Where condition some thing like this
UPDATE tb_Division SET LastModified = GetDate() where id=4
To just update LastModified on the rows that were affected by the insert or update you need to use the inserted
table which is only available in triggers.
ALTER TRIGGER [dbo].[DivisionLastModified]
ON [dbo].[tb_Division]
WITH EXECUTE AS CALLER FOR INSERT, UPDATE AS
BEGIN
UPDATE tb_Division
SET LastModified = GetDate()
WHERE tb_Division.<Primary Key Column Name Here> IN (
SELECT <Primary Key Column Name Here> FROM inserted
)
END
精彩评论