When a SQL statement is executed using the Execute command, it is not possible to obtain the rows affected count. At least I've not been able to find a way to obtain this value.
Example:
declare @sql nvarchar(1024), @rc int
set @sql="Update tbl Set ColA='Testing' where ID between 101 and 104"
Execute(@sql) /* or Exec(@sql) */
set @rc=@@rowcount
After executing this segment of T-SQL code, @rc does not contain the actual rows affected.
SQL Server 7 and 2000 provide a system-stored procedure named sp_executesql. SQL Books Online say this about sp_executesql:
"Executes a Transact-SQL statement or batch that can be reused many times, or that has been built dynamically. The Transact-SQL statement or batch can contain embedded parameters." (emphasis added)
sp_executesql has several advantages over the Execute command. One of the advantages is being able to determine the number of rows affected by the query.
Example:
declare @sql nvarchar(1024), @rc int
set @sql="Update tbl Set ColA='Testing' where ID between 101 and 104"
exec sp_executesql @sql
set @rc=@@rowcount
The value of @rc upon completion of this segment of T-SQL code will be the actual number of rows affected.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.