How to get row count from EXEC() in a TSQL SPROC?

DECLARE @rowcount TABLE (Value int); INSERT INTO @rowcount EXEC('SELECT COUNT(*) ' + @sqlBody); SELECT @ActualNumberOfResults = Value FROM @rowcount.

Perfect... thank you! – ElHaix May 31 at 20:57.

Keep in mind that dynamic SQL has its own scope. Any variable declared/modified there will go out of scope after your EXEC or your sp_executesql. Suggest writing to a temp table, which will be in scope to your dynamic SQL statement, and outside.

Perhaps put it in your sqlBottom: CREATE TABLE ##tempCounter(MyNum int); EXEC('SELECT @ActualNumberOfResults = COUNT(*) ' + @sqlBody + '; INSERT INTO ##tempCounter(MyNum) VALUES(@ActualNumberOfResults);'); SELECT MyNum FROM ##tempCounter.

Sp_executesql can use output parameters, see my example – SQLMenace May 31 at 20:37.

Use sp_executesql and an output parameter example DECLARE @sqlBody VARCHAR(500),@TableCount INT, @SQL NVARCHAR(1000) SELECT @sqlBody = 'from sysobjects' SELECT @SQL = N'SELECT @TableCount = COUNT(*) ' + @sqlBody EXEC sp_executesql @SQL, N'@TableCount INT OUTPUT', @TableCount OUTPUT SELECT @TableCount GO.

Any variable declared/modified there will go out of scope after your EXEC or your sp_executesql. Suggest writing to a temp table, which will be in scope to your dynamic SQL statement, and outside.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions