Tuesday, September 29, 2009

SQL Trigger to capture OLD and NEW values while updating in TABLE

CREATE TABLE [dbo].[Table1](
    [id] [int] NOT NULL,
    [ColumnA] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [namechanged] [int] NULL
) ON [PRIMARY]

GO

CREATE TRIGGER [table1_update] ON [dbo].[Table1]
FOR  UPDATE
AS begin
  update table1 set table1.namechanged = table1.namechanged + 1
  from inserted i, deleted d
  where i.[ColumnA] <> d.[ColumnA] and i.[id] = d.[id] and table1.[id] = i.[id]
end

No comments:

Post a Comment