I have a stored procedure that takes in a parameter @n and @colname and uses @n to compute some information. Right now @colname does nothing because T-SQL does not allow you to make a column name from a parameter. There are examples of people saying you can do this through a dynamic stored procedure, but as far as I can tell, they do not do what I want.
The reason behind this is because I want to create another stored procedure that uses this other stored procedure several times and passes different values of @n and @colname.
So just to clarify again, I would like to be able to write a dynamic stored procedure that does this:
SELECT a, b, c AS @colname
FROM t1
WHERE b = @n
Then once I can do that, I will write my other stored procedure like this:
EXEC stored_procedure1 @n = 3, @colname = 'Column 1'
EXEC stored_procedure1 @n = 6, @colname = 'Column 2'
Any help on this would be greatly appreciated. Thanks in advance
You need to make it a dynamic query like below
EXEC ('SELECT a, b, c AS ['+ @colname +
'] FROM t1 WHERE b = ' + @n)
Hope rest you can figure out.