I have this stored procedure that accept a comlumn name as inptu parameter. The SELECT statement will select a column according to input parameter
create procedure getColumn (@whichColumn varchar)
as
begin
declare @sql nvarchar(max)
set @sql = 'SELECT [' + @whichColumn + ']'
+ ' FROM myTable'
+ ' where ['+ @whichColumn + '] is not null'
+ ' and [' + @whichColumn + '] != '''' ' ;
exec sp_executesql @sql
end
When I execute this stored procedure,
exec getColumn 'Apple';
the error says "Invalid column name 'A' " . I cannot see why it only gets the first character of the input