Arvind Arvind - 7 months ago 11
SQL Question

Declaring a Variable as a Unique identifier is not working fine

I have written a simple SQL query where I declare a variable as an

UNIQUEIDENTIFIER
type, and I am trying to insert that into a table where the column is of
UNIQUEIDENTIFIER
type. But I get an error message saying


Conversion failed when converting from a character string to uniqueidentifier.


Can anyone help me with this?

Here's my code:

DECLARE @SessionId AS UNIQUEIDENTIFIER;
SET @SessionId='G51F4E30-E1AB-4E7E-8F5B-0E2613DC9001';

PRINT @SessionId;

INSERT INTO [DB_Name].[dbo].[table_name] ([SessionId])
VALUES (@SessionId);

Answer
SET @SessionId='G51F4E30-E1AB-4E7E-8F5B-0E2613DC9001';
                ^

'G' is not a valid hexadecimal character. GUIDs can only consist of characters from 0-9 and A-F.