tags:

views:

30

answers:

1

Hi All,

I wrote a trigger for a table for insert ,update. For every insert and update, in the trigger I am comparing rows from 'Inserted' table and rows from 'Deleted' table.

I need to get the affected column. How to do this?

A B C D
1 2 3 5.

I am updating B's value with 3. Then the trigger will fire. In that trigger, from deleted table I can get :

A B C D
1 2 3 5

From the Inserted table I can get:

A B C D
1 3 3 5

I need to get the column B alone.

How to do this?
Thanks.

+5  A: 

You can check whether or not a column has changed by IF UPDATE(namehere)

CREATE TRIGGER [dbo].[Triggername]
ON [dbo].[TableName]
FOR UPDATE
AS 

IF UPDATE(Columname) --If this column has changed
BEGIN
       --Your code here
    END
Sjuul Janssen
hah, I was half way through a long answer containing a horrible CASE statement. Had forgotton about UPDATE() func. Good answering.
Jamiec