September 9, 2015 at 7:26 am
Hello,
I have a framework where dynamic generated sql is executed like the following:
exec sp_executesql @p_command
set @p_affected_rows = @@rowcount
The query in @p_command was usually one statement. With the aim to make the transaction less big, I changed the sql to the following form:
while
process a chunk
end
This way, the method of retrieving @@rowcount will not work anymore. If I count the processed rows within my while-loop, can I somehow pass the sum to @@rowcount?
Thanks,
Tobias
September 9, 2015 at 7:27 am
No. @@rowcount returns the number of rows affected by the previous statement, that's it.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
September 9, 2015 at 7:35 am
You just accumulate the row counts. Be sure to initialize your variable to zero to avoid getting a null value.
while
process a chunk
set @p_affected_rows = @p_affected_rows + @@rowcount
end
September 9, 2015 at 8:45 am
Luis Cazares (9/9/2015)
You just accumulate the row counts. Be sure to initialize your variable to zero to avoid getting a null value.
while
process a chunk
set @p_affected_rows = @p_affected_rows + @@rowcount
end
Yes, the problem is, that this code will be contained parameter @p_command. I am looking for a way to get the rowcount after sp_executesql was executed. I was looking for an easy way without having to rewrite the framework, but this seems to be impossible.
September 9, 2015 at 10:10 am
sp_executesql allows for an OUTPUT parameter. You can see an example at sp_executesql (Transact-SQL)
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply