I have a table with 20 columns. I am dynamically selecting a column according to the user input and then want to display the selected column with it's data. Here is the code I wrote but the issue is I am getting column name as the data for the selected column. Could anyone please let me know what I am doing wrong here.
ALTER PROCEDURE Getreport
@Subject [varchar](10),
WITH EXECUTE AS CALLER
AS
--Query to generate report
SELECT FirstName, LastName, @Subject FROM Student
You need to use Dynamic SQL:
EXEC('SELECT FirstName, LastName,' + @Subject + ' FROM Student')