计算由存储过程返回的行数(Counting the number of rows returned

2019-08-07 22:36发布

怎样算一个存储过程将返回以最快的方式行数。 存储过程返回100K左右行1M记录。

Answer 1:

选择@@rowcount

SELECT @@ROWCOUNT;

执行存储过程之后。



Answer 2:

您可以定义输出变量:

create procedure x
    (@p1 int output)
as
    select @p1 = count(*) 
    from Table


Answer 3:

另一种方式来获得相同的结果

CREATE PROCEDURE NOMBRE_PROCEDIMIENTO
   as
BEGIN
   if EXISTS (SELECT * from NOMBRE_TABLA WHERE CONDITIONS HERE)
     BEGIN
       SELECT @@ROWCOUNT
     END
END


Answer 4:

Create procedure procedurename
AS
Begin

    Select * from Table --if you want where condition write here

End

Exec Procedurename

Select @@rowcount


Answer 5:

我有一个限制,类似的任务,我不能改变SP得到计数。 因此:

sp_configure 'show advanced options', 1;  
reconfigure;
go

sp_configure 'ad hoc distributed queries', 1;  
reconfigure;  
go

select count(*) from 
    openrowset('SQLOLEDB','Data Source=localhost;Trusted_Connection=yes;
    Integrated Security=SSPI','exec DBNAME..SPName')


文章来源: Counting the number of rows returned by stored procedure