Solved

Parameter in Select statement as column name

Posted on 2009-07-01
1
300 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
combine data in sql 1 29
SQL - sum months, quarter, YTD 9 81
MS SQL 2005 Srink database in chunks 4 40
SQL Query 2 57
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now