GUID part - 1
GUID part - 2
To check whether the given GUID is null, in SQL server, we have IS NULL keyword.
Declare @blog
If
Begin
Print 'The @blog
End
Else
Begin
Print 'The @blog
End
Where @blog is the variable. If we execute the above query obviously it prints 'The @blog
Now set the value to the variable @blog as follows.
Declare @blog
If
Begin
Print 'The @blog
End
Else
Begin
Print @blog
End
If we execute the above query obviously it prints the GUID value such as ' 17E2A682-F302-45BF-89E6-E46C0513D5D5' and if we you execute the query GUID value will be changed.
We can do the same thing as follows.
Declare @blog
If
Begin
End
--------------------------------------------------------------------------------------------------------------------------
Have a look at the following query.
Declare @blog
In the above query we know that @blog is null then NEWID
If you execute the query by giving value to the variable as follows, then @blog variable result will be returned,
Declare @blog
--------------------------------------------------------------------------------------------------------------------------
Empty GUID:
Generally empty GUID will be with all zero's as follows.
00000000-0000-0000-0000-000000000000
To get an empty GUID we need to use select statement as follows.
1) SELECT CAST( CAST( 0 AS BINARY) AS UNIQUEIDENTIFIER)
2) SELECT CAST( 0x0 AS UNIQUEIDENTIFIER)
We can use any one of the above. The following are the two scenarios to check the GUID is empty.
1) By comparing with empty.
Declare @blog UniqueIdentifier
Set @blog = '00000000-0000-0000-0000-000000000000'
If( @blog = '00000000-0000-0000-0000-000000000000')
Begin
Print 'The @blog Guid is Empty'
End
Else
Begin
Print 'The @blog Guid is not Empty'
End
In the above scenario the result is that 'The @blog Guid is Empty'
2) By using CAST
Declare @blog UniqueIdentifier
Set @blog = '00000000-0000-0000-0000-000000000000'
If( @blog = Cast( 0x0 as Uniqueidentifier ))
Begin
Print 'The @blog Guid is Empty'
End
Else
Begin
Print 'The @blog Guid is not Empty'
End
In the above scenario the result is that 'The @blog Guid is Empty'
The latest example can be rewritten as below (we are talking about T-SQL, right?):
ReplyDeleteDeclare @blog UniqueIdentifier
Set @blog = '00000000-0000-0000-0000-000000000000'
If(@blog = 0x0)
Begin
Print 'The @blog Guid is Empty'
End
Else
Begin
Print 'The @blog Guid is not Empty'
End
Please note 0x0 is the hexadecimal equivalent of the empty guid.