SELECT @COLNAME FROM TABLE....

Hi,
I'm trying to obtain the value of a column as the column name is passed as a variable:
sample:

declare @colname varchar(25)
set @colname = 'COL1'
-- COL1 IS A column of TABLE1

SELECT @colname from TABLE1

THE problem is that this query return 'COL1' and NOT the value of 'COL1'

Thanks for your help....

bruno_boccaraAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee SavidgeCommented:
Hi,

Try some dynamic SQL:

declare @colname varchar(25)
set @colname = 'COL1'
-- COL1 IS A column of TABLE1

exec ('SELECT ' + @colname + ' from TABLE1')

Cheers,

Lee
0
bruno_boccaraAuthor Commented:
Hi,
the problem is that the query is in fact a subquery and it dowsnt work....

sample:

SELECT col3, col4, exec ('SELECT ' + @colname + ' from TABLE1' where cond) from TABLE2 WHERE cond

Regards.
0
Lee SavidgeCommented:
Hi,

Then do the whole thing as dynamic SQL

exec ('SELECT col3, col4, (SELECT ' + @colname + ' from TABLE1 where cond) from TABLE2 WHERE cond')

Regards,

Lee
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Jinesh KamdarCommented:
Then include the entire query in the EXEC.

declare @colname varchar(25)
set @colname = 'COL1'

exec ('SELECT col3, col4, (SELECT ' + @colname + ' from TABLE1 where cond) from TABLE2 WHERE cond')
0
Jinesh KamdarCommented:
Oops, guess Lee was faster than i thought ;)
0
Jinesh KamdarCommented:
Hey, you accepted the wrong solution. Lee deserved the points !!! Pls get CS to rectify this ASAP !!!
0
bruno_boccaraAuthor Commented:
OOOPPPPS

I made a mistake , I wanted to give points to Lee....

Lee , I'm creating the same question and give you the points.

Sorry for the mistakes.....
0
bruno_boccaraAuthor Commented:
Get CS ? what is it ??
how to rectify ??
0
Jinesh KamdarCommented:
Post a 0-point question in this zone : http://www.experts-exchange.com/Other/Community_Support
Explain how you want them to close this question instead of the current scenario.
Don't forget to paste the URL of this question in ur rectify request.
0
bruno_boccaraAuthor Commented:
I JUST SENT AN EMAIL TO THE CS....
0
bruno_boccaraAuthor Commented:
Many Thanks.
Sorry for the mistake.....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.