lm1189
asked on
Variables as Column Names
Can you declare a variable, set its value as a column name and pull based on the column?
IE:
The select query should pull Column1 from mytable. The idea is I want to build a script that would be easily reusable simply by changing the variables.
IE:
DECLARE @WIDGET, varchar(255) = 'COLUMN1'
SELECT @WIDGET FROM MYTABLE
The select query should pull Column1 from mytable. The idea is I want to build a script that would be easily reusable simply by changing the variables.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
that could lead to SQL injection
Not a good idea. Poor caching, poor security, poor performance.
DECLARE @WIDGET, varchar(255) = 'COLUMN1'
SELECT @WIDGET
OR
SELECT @WIDGET, * FROM MYTABLE
SELECT @WIDGET
OR
SELECT @WIDGET, * FROM MYTABLE