We help IT Professionals succeed at work.

SQL Keyword Key - But also column name

Hi all,

I have a column named Key - but when trying to do this:
declare @userID varchar(15)
@userID = (SELECT Key from inserted)

I get an error as "Key" is a keyword, how do I get the info I want from the column and get around this issue with the keyword. As some further help this is the design of the table:

[Key] varchar(15)


I am really new to SQL so any help would be great.


Dan
Comment
Watch Question

Top Expert 2011
Commented:
it is keyword.

You can walk around it by wrapping it with []

declare @userID varchar(15)
@userID = (SELECT [Key] from inserted)
NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
Dan

You can probably get round this by enclosing in [].

But why not rename the column to avoid any further problems?

Author

Commented:
SET @userID = (SELECT [Key] from inserted)
gives:Invalid column name 'Key'.

@imnorie I am so new I do not dare touch it just yet :) I dont want to tamper with a external persons DB which I am applying the trigger on.
Top Expert 2011
Commented:
then that key doesn't exist on the db or it belongs to a different table.

otherwise, it would have worked
NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
Enclosing in [] works for me.
NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
By the way, where are you running this code?

Commented:
<< from inserted>>
it will be in a trigger

but <<@imnorie I am so new I do not dare touch it just yet :) I dont want to tamper with a external persons DB which I am applying the trigger on.>>
say's otherwise

Explore More ContentExplore courses, solutions, and other research materials related to this topic.