How do I check if an argument in a stored procedure is an empty GUID or not?
-- If you want to generate a new Guid (uniqueidentifier) in SQL server the you can simply use the NEWID() function. -- This will return a new random uniqueidentifier e.g. You can directly use this with INSERT statement to insert new row in table.
You can use Guid.Empty . It is a read-only instance of the Guid structure with the value of 00000000-0000-0000-0000-000000000000. you can also use these instead. var g = new Guid(); var g = default(Guid);
SELECT CAST(CAST(0 AS BINARY) AS UNIQUEIDENTIFIER)
That should return your empty guid.
... or even shorter, saving one cast:
SELECT CAST(0x0 AS UNIQUEIDENTIFIER)
So to check for that, you would do
IF @GuidParam = CAST(CAST(0 AS BINARY) AS UNIQUEIDENTIFIER) BEGIN --Guid is empty END
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With