Link to home
Start Free TrialLog in
Avatar of kcmoore
kcmooreFlag for United States of America

asked on

Parameter in Select statement as column name

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

ASKER CERTIFIED SOLUTION
Avatar of pssandhu
pssandhu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial