In a stored procedure (using SQL Server 2008 R2 SP2) is it possible to return a NewSequentialID()
without a temp table variable?
I can successfully obtain the NewSequentialID()
by using a temp table:
Getting Value of NEWSEQUENTIALID() on Insert
Perhaps I’m old school, but I try to refrain from using temp tables unless absolutely necessary… though this might be a case where it is absolutely necessary…
IF I try:
DECLARE @NewSequentialID UNIQUEIDENTIFIER;
SET @NewSequentialID = NEWID()
… it works as expected.
IF I try:
DECLARE @NewSequentialID UNIQUEIDENTIFIER;
SET @NewSequentialID = NEWSEQUENTIALID()
… I receive the following error:
The newsequentialid() built-in function can only be used in a DEFAULT expression for a column of type ‘uniqueidentifier’ in a CREATE TABLE or ALTER TABLE statement. It cannot be combined with other operators to form a complex scalar expression.
- Is the ONLY solution to use a temp table method?
- Does anyone know of a reason why Microsoft implemented a difference between
NEWSEQUENTIALID()
to work likeNEWID()
? - Anyone know if there's a chance Microsoft will update
NEWSEQUENTIALID()
to work likeNEWID()
?
Geo
UPDATE --
I'm not sure why Microsoft choose to implement the method in this manner, since they state that, "NEWSEQUENTIALID is a wrapper over the Windows UuidCreateSequential function"... but it appears that there is no non-temp-variable table method. (At least as of yet.)
Thanks for everyone's comments / answers. [Moderator Note:] I'm not sure what to do with a question when the answer is "not possible". So I'm going to give @marc_s credit for detailing a workaround.