Manoj
Manoj

Reputation: 5867

How to select dynamically column from the table?

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.

Upvotes: 1

Views: 297

Answers (1)

Sjuul Janssen
Sjuul Janssen

Reputation: 1802

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

Upvotes: 5

Related Questions