?
Solved

Parameter in Select statement as column name

Posted on 2009-07-01
1
Medium Priority
?
348 Views
Last Modified: 2012-05-07
I'm trying to select a specific column name from a table based on a string from a parameter.
How can I do this?
Code below...what I am trying to return is basically,

Select QA1 from @TEMP
if @QA = QA1.
DECLARE @VAL INT
	SET @VAL = 2
 
DECLARE @QA VARCHAR(5)
	SET @QA = 'QA'+CAST(@VAL AS VARCHAR(1))
 
DECLARE @TEMP TABLE
( PK INT, QA1 VARCHAR(4), QA2 VARCHAR(4) )
INSERT INTO @TEMP
	SELECT 1,'ASDF','JHFJ'
select * from @temp
 
SELECT @QA
FROM @TEMP

Open in new window

0
Comment
Question by:kcmoore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 17

Accepted Solution

by:
pssandhu earned 150 total points
ID: 24757859
You have to use dynamic sql. Something like this for your select statment in the end
exec ('SELECT '+ @QA + ' FROM @TEMP')
P.

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

762 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