我有一个存储过程,它将列名和表名作为参数。
ALTER PROCEDURE [dbo].[usp_TableReporting_GetColumnNamesAndValues] @concatstring AS VARCHAR(100), @Tablename as VARCHAR(100) AS BEGIN SELECT @concatstring FROM @Tablename END
此过程给我一个错误:
必须声明标量变量@TableName
尽管我已经在顶部声明了它。
使用动态SQL:
ALTER PROCEDURE [dbo].[usp_TableReporting_GetColumnNamesAndValues] @concatstring AS VARCHAR(100), @Tablename as VARCHAR(100) AS BEGIN Declare @mystring varchar(max) = N'select ' + @concatstring + ' from ' + @Tablename exec (@mystring) END