SQL TRIGGER update is used when you want to do something with the updated value (old value). In this post I will give you a simplified example of SQL Trigger after update that I used recently. I will use SQL Server 2008 R2.
In my case I have page table with a page field called PageText where I keep blog post content. I want to move old value into PageHistory so I use in the future. To do that I create a trigger and example is below.
CREATE TRIGGER dbo.trgPageUpdateToHistory--in here we just put trigger name
ON dbo.Page --we need to specify which table should get our new trigger
AFTER UPDATE --we need to specify when to execute the trigger and this case it is after update
SET NOCOUNT ON;
-- Insert statements for trigger which will inserts old value to PageHistory table
INSERT INTO dbo.PageHistory (ID, PageTxt)
SELECT d.ID, d.PageText
FROM deleted AS d -- here is the tricky part when you perform AFTER UPDATE you get access to old value using deleted and new values using inserted
This was a simple real life example… I have actually used this trigger on my database with other fields but unfortunatelly there is a limitation. My PageContent field is actually TEXT data type and not for instance nvarchar where I would exceed the limit and in my case so I couldn't put my PageContent Field to history which is a little bit of a shame but I can live without it.
I hope this example was simple and easy to follow. If you have any questions on SQL trigger update feel free to add comment.
Hope that helps