简体   繁体   English

如何在SQL Server 2005中标识空白唯一标识符?

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

I'm getting a uniqueidentifier into a Stored Procedure that looks like this 我正在将一个uniqueidentifier变成一个看起来像这样的存储过程

00000000-0000-0000-0000-000000000000 . 00000000-0000-0000-0000-000000000000

This seems like a simple thing, but how can identify that this is a blank uniqueidentifier ? 这看起来很简单,但如何确定这是一个空白的uniqueidentifier

If I get a value like this DDB72E0C-FC43-4C34-A924-741445153021 I want to do X 如果我得到像这样的值DDB72E0C-FC43-4C34-A924-741445153021我想做X

If I get a value like this 00000000-0000-0000-0000-000000000000 I do Y 如果我得到这样的值00000000-0000-0000-0000-000000000000我做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: 只需创建一个EmptyGuid变量并与之进行比较:

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 最佳解决方案是为空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 注意:您不需要使用强制转换和转换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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