简体   繁体   中英

How do I identify a blank uniqueidentifier in SQL Server 2005?

I'm getting a uniqueidentifier into a Stored Procedure that looks like this

00000000-0000-0000-0000-000000000000 .

This seems like a simple thing, but how can identify that this is a blank uniqueidentifier ?

If I get a value like this DDB72E0C-FC43-4C34-A924-741445153021 I want to do X

If I get a value like this 00000000-0000-0000-0000-000000000000 I do Y

Is there a more elegant way then counting up the zeros?

Thanks in advance

compare to

cast(cast(0 as binary) as uniqueidentifier)

?

Just create an EmptyGuid variable and compare against that:

DECLARE @EmptyGuid UniqueIdentifier
SET @EmptyGuid = '00000000-0000-0000-0000-000000000000'
IF (@TheGuid = '00000000-0000-0000-0000-000000000000')
    SELECT 'Do Y'
ELSE
    SELECT 'Do X'

This also works.

DECLARE @EmptyGuid UNIQUEIDENTIFIER = CONVERT(UNIQUEIDENTIFIER, 0x0);  
SELECT @EmptyGuid

Best solution is to use a constant for the empty GUID

DECLARE @EmptyGuid UNIQUEIDENTIFIER
SET @EmptyGuid = '00000000-0000-0000-0000-000000000000'

OR

DECLARE @EmptyGuid UNIQUEIDENTIFIER
SET @EmptyGuid = 0x0

and you just compare them

IF @parameter = @EmptyGuid
    DO Y
ELSE
    DO X

Note: you don't need to use casts and converts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM