Dima
Dima

Reputation: 1761

How to know new value of primary key in UPDATE trigger? (T-SQL)

I have a table:

CREATE TABLE Demo ( id uniqueidentifier PRIMARY KEY );

I want to create an AFTER UPDATE (or INSTEAD OF UPDATE) trigger, and I need to know OLD and NEW values of "id" column inside the trigger.

Is it possible to do this?

Upvotes: 2

Views: 646

Answers (1)

Martin Smith
Martin Smith

Reputation: 453028

No. This isn't possible.

If the table has 2 rows and you update both of them there is no way of knowing which row in INSERTED maps to which in DELETED.

You can use the OUTPUT clause for this (outside a trigger) though.

Or you could add an IDENTITY column to the table and use that to join INSERTED and DELETED (it is not permitted to update identity columns so that gives you something immutable)

Upvotes: 2

Related Questions