Thursday, December 30, 2010

MS SQL GUID lengths

Working with MS SQL and found out that errors with GUIDs are only thrown if the length of the GUID isn't met. The length of the GUID can be over the length of a regular GUID. This is kinda wierd because one would think this would error, but in fact MS SQL actually only will store the length of the GUID String up to the length of a normal GUID. Check out the examples below...


create table Snowboarder(
id uniqueidentifier not null,
name varchar(200) not null,
primary key (id)
)

/*--will throw an error---*/
insert into Snowboarder(
id,
name
)values(
'',
'Blank GUID'
)

/*--will work---*/
insert into Snowboarder(
id,
name
)values(
'D832DE4B-8453-4831-9505-6CA8CB4A4033',
'Regular GUID'
)

/*--will work, but it will only store the first guid---*/
insert into Snowboarder(
id,
name
)values(
'FE7A6068-080B-43B3-B392-3ABCE2E5FADF13725D91-B434-41B4-97A8-5607BF864EF5',
'Two GUIDS together'
)

/*--will throw an error because the first part is already in the db--*/
insert into Snowboarder(
id,
name
)values(
'D832DE4B-8453-4831-9505-6CA8CB4A40336448153D-A72F-42A8-BB35-17EE076D393C',
'Two GUIDS together with the first part already in the db'
)

No comments:

Post a Comment