Solved

Parameter in Select statement as column name

Posted on 2009-07-01
1
325 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
1 Comment
 
LVL 17

Accepted Solution

by:
pssandhu earned 50 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 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