Avatar of EastThames
EastThames
Flag for United Kingdom of Great Britain and Northern Ireland asked on

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
Microsoft SQL Server 2005SQL

Avatar of undefined
Last Comment
jogos

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
sammySeltzer

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Norie

Dan

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

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

ASKER
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.
SOLUTION
sammySeltzer

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Norie

Enclosing in [] works for me.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Norie

By the way, where are you running this code?
jogos

<< 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