Jeremy T Jeremy T - 6 months ago 14
SQL Question

Select row in SQL using GUID

When creating the table I used

[Key] UNIQUEIDENTIFIER not null DEFAULT NEWSEQUENTIALID(),


I'm trying to use the following to select, I gave up on doing it in C# for now and am entering the query in MyLittleAdmin (the SQL database manager my hosting provider is using)

SELECT *
FROM GameList
WHERE Key = '9abc2cdc-1919-e611-80d3-008cfa5ae917'


I get the following error:


Msg 156, Level 15, State 1, Line number 1

Incorrect syntax near the keyword 'Key'.


I know I have the right GUID, using
SELECT * FROM GameList
returns the data and I have copied and pasted from it.

Answer

Key is a keyword and must be surroundted by string identifiers if you want to use it as a column name (like it is in the column definition):

SELECT * 
FROM GameList  
WHERE [Key] = '9abc2cdc-1919-e611-80d3-008cfa5ae917'