I am using SQL Server 2008 with GUIDs in all my tables and using some stored procedures. I want to know if there is a way to output the current GUID that you are updating to?
I know there is the codes below that I use in other procedures:
output inserted.*
output deleted.*
However, I have a case that is an if null insert, else, update. I would like to know which GUID was used in each case. I have heard another way where you create another table, set the value in the table to that the just updated GUID and then SELECT the value from the newly created table, but that just seems extremely redundant and unnecessary. Any workarounds?
Thanks!
Do you have newid() as the default for the column or are you generating a guid in your code when you do insert/update?
If you have a default on it of NewID() then there isn’t a way to grab the last inserted/updated one, had it been identity you could have used @@IDENTITY to get the last inserted ID.
Only way I can think of to do what you want is to take the default off the column, generate a NewID() in your code for insert and remember what that was.
(This answer was made with the assumption you’re using GUIDS as PK’s)