I had the same issue located here Table name as variable
My question is, how can I store the results returned from the EXEC statement into a @variable ?
Example:
EXEC('SELECT count(*) FROM ' + @tablename)
Thanks
I had the same issue located here Table name as variable
My question is, how can I store the results returned from the EXEC statement into a @variable ?
Example:
EXEC('SELECT count(*) FROM ' + @tablename)
Thanks
write as:
DECLARE @SQLString NVARCHAR(500)
DECLARE @ParmDefinition NVARCHAR(500)
DECLARE @COUNT INT
DECLARE @TableName NVARCHAR(100)
SET @SQLString = N'SELECT @COUNTOUT = count(*) FROM ' + QUOTENAME(@TableName);
SET @ParmDefinition = N'@COUNTOUT INT OUTPUT'
EXECUTE sp_executesql
@SQLString,
@ParmDefinition,
@COUNTOUT=@COUNT OUTPUT
SELECT @COUNT