这没有任何问题:
select COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,IS_NULLABLE
from [AdventureWorks2012].INFORMATION_SCHEMA.COLUMNS WHERE table_name='employee';
但是,当它移动到动态语句时,它总是失败:
begin
DECLARE @sqlstatement VARCHAR(MAX);
DECLARE @TableName varchar(max)='employee';
set @sqlstatement='select COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,IS_NULLABLE
from [AdventureWorks2012].INFORMATION_SCHEMA.COLUMNS WHERE table_name='+@TableName;
print (@sqlstatement);
EXECUTE(@sqlstatement);
end
错误说:无效的列名“员工”