Arvind Arvind - 1 year ago 38
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

type, and I am trying to insert that into a table where the column is of
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:

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

PRINT @SessionId;

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

Answer Source
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.