wilest wilest - 6 months ago 21
SQL Question

T SQL After Update Trigger on column get the value changed

I've only started exploring with triggers recently. I've set up my database mail function and all is working well. My trigger works perfectly if the column value is changed, but how do I get the value/row that changed?

USE [database]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[triggerName] ON [dbo].[tableName]
FOR UPDATE
AS

declare @cust varchar(100);


if update(Column)
BEGIN
SET NOCOUNT ON
set @cust = THE VALUE MUST GO HERE I ASSUME
END
EXEC msdb.dbo.sp_send_dbmail
@recipients = 'someone@example.com',
@profile_name = 'ProfileName',
@subject = 'Customer Information Changed',
@body = @cust;

Answer

Compare the values in inserted and deleted. If they are not the same, then the value changed.

Comments