[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

SELECT @COLNAME FROM TABLE....

Posted on 2008-02-06
12
Medium Priority
?
1,312 Views
Last Modified: 2012-06-22
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....

0
Comment
Question by:bruno_boccara
  • 5
  • 4
  • 2
11 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 20833770
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
 

Author Comment

by:bruno_boccara
ID: 20833829
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
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 2000 total points
ID: 20833872
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 20833879
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
 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 20833889
Oops, guess Lee was faster than i thought ;)
0
 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 20833963
Hey, you accepted the wrong solution. Lee deserved the points !!! Pls get CS to rectify this ASAP !!!
0
 

Author Comment

by:bruno_boccara
ID: 20833967
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
 

Author Comment

by:bruno_boccara
ID: 20833975
Get CS ? what is it ??
how to rectify ??
0
 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 20834120
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
 

Author Comment

by:bruno_boccara
ID: 20834200
I JUST SENT AN EMAIL TO THE CS....
0
 

Author Closing Comment

by:bruno_boccara
ID: 31428553
Many Thanks.
Sorry for the mistake.....
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Article by: Tammy
MySQLTuner is a script written in Perl that allows you to review a MySQL installation quickly and make adjustments to increase performance and stability. The current configuration variables and status data is retrieved and presented in a brief forma…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question