0

I have a table with uniqueidentifier column. Insert script looks like this :

INSERT INTO [Users] ([UserId], [Name], [Surname]) VALUES (NEWID(), 'SomeName', 'SomeSurname')

NEWID() creates new GUID, which is inserted to table. I would like to create some data with defined id column, not automatically generated. E.g. 'a0000000-0000-0000-0000-000000000000'. This code doesn't work because it throws error 'String or binary data would be truncated.' :

INSERT INTO [Users] ([UserId], [Name], [Surname]) VALUES ('a0000000-0000-0000-0000-000000000000', 'SomeName', 'SomeSurname')

I tried cast this custom guid, but I was not successful as well.

INSERT INTO [Users] ([UserId], [Name], [Surname]) VALUES (CAST('a0000000-0000-0000-0000-000000000000' AS uniqueidentifier), 'SomeName', 'SomeSurname')

Do you have any clue how I can solve inserting data with custom defined ID?

y0j0
  • 3,369
  • 5
  • 31
  • 52

1 Answers1

1

You don't have to cast it. SQL Server will do it for you.

This code works well:

create table x
( a uniqueidentifier
);

insert into x values ('A0000000-0000-0000-0000-000000000000')
;

insert into x values ('BBE46A77-3518-40E4-9B77-3275F3531B8B')
;

select *
from x
;
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325