I have a stored procedure which takes the column name as well as the table name as a parameter.
ALTER PROCEDURE [dbo].[usp_TableReporting_GetColumnNamesAndValues]
@concatstring AS VARCHAR(100),
@Tablename as VARCHAR(100)
AS
BEGIN
SELECT @concatstring FROM @Tablename
END
This procedure gives me an error :
Must declare the scalar variable @TableName
although I have already declared it on the top.
use dynamic 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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments