3

I've disabled row counting using SET NOCOUNT ON but it seems that sqlserver is still counting rows.

USE Northwind
SET  NOCOUNT ON;
SELECT * FROM Products p
SELECT @@ROWCOUNT AS 'RowCount';

the query returns 77 for row count, why?

4

2 回答 2

6

SET NOCOUNT ON;

prevents the rowcount from being returned when you execute your stored procedure. It has no affect on @@rowcount.

see http://msdn.microsoft.com/en-us/library/ms189837.aspx for specific info

The @@ROWCOUNT function is updated even when SET NOCOUNT is ON.

SET NOCOUNT ON prevents the sending of DONE_IN_PROC messages to the client for each statement in a stored procedure. For stored procedures that contain several statements that do not return much actual data, or for procedures that contain Transact-SQL loops, setting SET NOCOUNT to ON can provide a significant performance boost, because network traffic is greatly reduced."

于 2012-10-10T21:58:54.857 回答
1

nocount设置不会阻止数据库计算行数,它会阻止它返回结果集updateinsert查询。

例子:

create procedure test1
as
insert into Products (ProductName) values ('Fork')
select scope_identity() as ProductId

调用该过程将返回两个结果集,第一个是空的并且包含受影响行数为 1 的信息,第二个包含具有产品 ID 的记录。

与:

create procedure test1
as
set nocount on
insert into Products (ProductName) values ('Fork')
select scope_identity() as ProductId

调用该过程将只返回一个包含产品 ID 的结果集。

于 2012-10-10T22:00:34.503 回答