Returning the Value of a Uniqueidentifier after an Insert in SQL Server

02 Feb 2005

Something I looked-up just to confirm what I already knew-- there isn't a way to automagically get the value of a GUID after inserting into SQL Server by using something similar to SELECT @@identity as you would with normal identity column. Instead you can do this:

Now, unlike an IDENTITY column, a uniqueidentifier column doesn't automagically get an assigned value when a row is inserted into a table. You either need to place a default on the uniqueidentifier column (DEFAULT NEWID()), or do something like the following:

DECLARE @GUID uniqueidentifier SET @GUID = NEWID() INSERT Item VALUES (@GUID,'Yak Hoof')

As a sidenote, I've not worked in SQL Server nearly as much the past couple of years as I have previously because our primary database platform is something different at work, so there are a few things I'm rusty with for sure.