Key int, Val1 int, Val2 int, val3, int, -- -- val40 intThe actual table has a ton of columns with weird names. I did not create the table, nor the program, just inherited and must finish it what somebody else has left.
DECLARE @Val1 INT, @Val2 INT, @Val3 INT, @Val4 INT, @Val5 INT SELECT @Val1=Val1, @Val2=Val2, @Val3=Val3, @Val4=Val4, @Val5=Val5 -- and a lot more FROM table WHERE -- conditionIf I am now passsed a parameter:
@Column2Get CHAR(20)
SELECT @(whatever_var)=(whatever_column) FROM table WHERE -- conditionSure, I could do an IF.ELSE
IF @Column2Get='Val1' BEGIN SELECT @Val1=Val1 FROM table WHERE -- condition END ELSE BEGIN IF @Column2Get='Val1' BEGIN SELECT @Val2=Val2 FROM table WHERE -- condition END ELSE -- another 40 times! ENDIs there a more elegant solution to build the query on the fly, retrieving the proper column, in a stored procedure?