Register
Sunday, February 05, 2012
 
 DBAs And ProgrammersBlog
  
News! Minimize
   
 
 Print   
 
Misc Blog Stuff Minimize
   
 
 Print   
 
The Reluctant DBA Minimize
 
 
 
 Print   
 
Reluctant DBA Minimize
   
 
  
 
Reluctant DBA Minimize
   
 
  
 
The Reluctant DBA Minimize
 
Jun18

Written by:CarpDeus
6/18/2008 8:02 AM 

An Universally Unique Identifier (UUID) can be a handy thing to use. They are guaranteed to be unique across time and space (or very nearly so) and make great identifiers that you can be fairly sure won't be duplicated in another table like an Identity column can. But they do pose some challenges.

Empty UUIDs, for instance. C# can use Guid.Empty.ToString() to return 00000000-0000-0000-0000-000000000000. But it can be a pain having to type that out. So it's easy to create a UDF that will return an empty UUID:

-- =============================================
-- Author:        Josef Finsel
-- Create date: 6/18/20008
-- Description:    Return a zero filled UUID
-- =============================================
CREATE FUNCTION EmptyUUID
()
RETURNS UniqueIdentifier
AS
BEGIN
    RETURN convert(uniqueidentifier,  '00000000-0000-0000-0000-000000000000')
END
GO

However, I try not to use this in my actual code (see Why Functions Can Be Bad for more details). SQL should be smart enough to evaluate once and be done with it. But a zero filled UUID should never change so I generally use the EmptyUUID to get the zero filled string and paste that into my code.

The other challenge with UUIDs is that using them as parameters defined as the uniqueidentifier data type can cause challenges in programming with C#. So I usually code them as char(36) in stored procedure parameters. But that means that other data can be passed in, including empty strings, invalid UUIDs, etc. So, what's the easiest way to determine a valid UUID in SQL Server?

CREATE FUNCTION [IsGuid](@guid nvarchar(36)) RETURNS INT AS
begin
    declare @rtn int
    if (@guid
        like
            replicate('[0-9a-fA-F]', 8) + '-' +
            replicate('[0-9a-fA-F]', 4) + '-' +
            replicate('[0-9a-fA-F]', 4) + '-' +
            replicate('[0-9a-fA-F]', 4) + '-' +
            replicate('[0-9a-fA-F]', 12)
        )
        set @rtn = 1
    else
        set @rtn = 0
    --endif
    return @rtn
end

Of course, it'd be so much easier if SQL Server actually supported a boolean data type. But this creates a like comparator of:

[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]-[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]-[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]-[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]-[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]

I actually have the string rather than the replicate statement (which is commented out in my code)  so that SQL doesn't have to calculate it every time but the replicate is shorter to display for code purposes.

That's enough to serve as an introduction to UUIDs.

Tags:

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment  Cancel 
 
 
  
 
Privacy Statement | Terms Of Use Copyright 2001-2008 by ReluctantDBA.com