我有这个存储过程接受comlumn名称作为inptu参数.SELECT语句将根据输入参数选择一列
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
Run Code Online (Sandbox Code Playgroud)
当我执行这个存储过程时,
exec getColumn 'Apple';
Run Code Online (Sandbox Code Playgroud)
错误显示"无效的列名称'A'".我不明白为什么它只获得输入的第一个字符