Determine Old primary key in a SQL Trigger

I've done this before somewhere I'm sure of it!

I have a SQL Server 2000 table that I need to log changes to fields on updates and inserts into a second Logging table. A simplified version of the structure I'm using is below:

MainTable
ID varchar(10) PRIMARY KEY
DESCRIPTION varchar(50)

LogTable
OLDID varchar(10)
NEWID varchar(10)

For any other field something like this would work great:

Select i.DESCRIPTION As New, d.DESCRIPTION As Old 
From Inserted i
LEFT JOIN Deleted d On i.ID=d.ID

...But obviously the join would fail if ID was changed.

I cannot modify the Tables in way, the only power I have in this database is to create a trigger.

Alternatively is there someone who can teach me time travelling and I'll go back into the past and ask myself back then how I did this? Cheers :)


Edit:

I think I need to clarify a few things here. This is not actually my database , it is a pre-existing system that I have almost no control of, other than writing this trigger.

My question is how can I retrieve the old primary key if said primary key was changed. I don't need to be told that I shouldn't change the primary key or about chasing up foreign keys etc. That's not my problem :)


是否有可能假定在触发器中向您呈现的INSERTED和DELETED表保证按照相同的顺序?


DECLARE @OldKey int, @NewKey int;

SELECT @Oldkey = [ID] FROM DELETED;
SELECT @NewKey = [ID] FROM INSERTED;

This only works if you have a single row. Otherwise you have no "anchor" to link old and new rows. So check in your trigger for > 1 in INSERTED.


I don't think it's possible. Imagine if you have 4 rows in the table:

1  Val1
2  Val2
3  Val3
4  Val4

Now issue the following update:

UPDATE MainTable SET
ID = CASE ID WHEN 1 THEN 2 WHEN 2 THEN 1 ELSE ID END
Description = CASE ID WHEN 3 THEN 'Val4' WHEN 4 THEN 'Val3' ELSE Description END

Now, how are you going to distinguish between what happened to rows 1 & 2 and what happened to rows 3 & 4. And more importantly, can you describe what's different between them? All of the stuff that tells you which columns have been updated won't help you.

If it's possible in this case that there's an additional key on the table (eg Description is UNIQUE), and your update rules allow it, you could write the trigger to prevent simultaneous updates to both keys, and then you can use whichever key hasn't been updated to correlate the two tables.

链接地址: http://www.djcxy.com/p/95662.html

上一篇: 如何在TSQL Select中为每一行生成随机数?

下一篇: 确定SQL触发器中的旧主键