Wednesday, March 7, 2012

how can I get which columns were updated in trigger on update

Hi,

I'm using sql-2005.

I want to update several columns in different tables whenever an update is happend on specific table. I have a trigger for that which update all the relevant places when it fires.

I want to do this update only if certains columns were changed, otherwise - do anything, to reduce performance.

How Can I know which columns were updated inside the trigger? The tables has many columns and only if 2 of them were changed then I need to update other tables.

Many thanks,

Nira.

use Inserted & Deleted table on your Trigger to compare wheather your action columns are updated or not.

Suppose I have table Table1 with 3 columns, Id, col1, col2, col3

if i need to find from trigger wheather col1 & col3 are updated or not,

Declare @.Col1Flag as Bit, @.Col2Flag as Bit

Select

Col1Flag = Case When Ins.Col1 <> Del.Col1 Then 1 Else 0 End Col1_Updated

,Col2Flag = Case When Ins.Col3 <> Del.Col3 Then 1 Else 0 End Col3_Updated

From Inserted as Ins

Join Deleted as Del on Ins.Id = Del.Id;

|||

Many thanks for you quick response

Though maybe there is some build-in way to do that but that's definitely a simple good way.

Thx

No comments:

Post a Comment