1

I have a table containing SQL statements stored as VARCHAR. I want to count how many rows each statement returns. I'm reading the statements into a temp variable called @SQL, and doing something like this as I cursor through each row:

set @SQL = (select sqlcode from t1 where id = @id)  
set @SQL = 'Select @id, count(*) from (' + @SQL + ') as t';

insert into #myresults

exec(@SQL)

This works just fine unless until one of the SQL statements itself uses temp tables or cursors.

Andrea
  • 11,801
  • 17
  • 65
  • 72

1 Answers1

0

Assign the count to a variable

DECLARE @mycount int
INSERT INTO #TempTable(ColumnList)
SELECT ColumnList From SomeplaceElse
SET @mycount =@@ROWCOUNT
Sev
  • 761
  • 4
  • 16
  • 29
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Rosário Pereira Fernandes Sep 19 '17 at 03:20