仅在存储过程中显示@@ RowCount

时间:2018-02-02 16:01:52

标签: sql-server stored-procedures

运行存储过程时,有没有办法只显示@@RowCount

我的SP看起来像这样:

ALTER PROCEDURE [dbo].[sp_ClientCount] (
@DateFrom AS DATETIME, 
@DateTo AS DATETIME,
@ClientCount as INT = NULL 
) AS
select tblClients.ClientID, count(*)
   FROM   tblReqPayment INNER JOIN
          tblClients ON tblReqPayment.ClientID = 
tblClients.ClientID
   WHERE  BETWEEN @DateFrom and @DateTo
          GROUP By tblClients.ClientID
   HAVING count(*) = @ClientCount

   SELECT @@ROWCOUNT as 'Count'

但是,我看到了我的初始SELECT语句以及SELECT @@RowCount语句的结果。有没有办法只显示@@RowCount

2 个答案:

答案 0 :(得分:3)

你可以稍微简化一下。这样的事情。

ALTER PROCEDURE [dbo].[sp_ClientCount] 
(
    @DateFrom AS DATETIME, 
    @DateTo AS DATETIME,
    @ClientCount as INT = NULL 
) AS
    set nocount on;

    select count(*) as ClientCount
    from
    (
        select count(*)
        FROM tblReqPayment p
        INNER JOIN tblClients c ON p.ClientID = c.ClientID
        WHERE ??? BETWEEN @DateFrom and @DateTo
        GROUP By c.ClientID
        HAVING count(*) = @ClientCount
    ) x

你发布的where子句只有谓词所以应该是什么。此外,您应该考虑不使用前缀sp_(或者更好的是根本没有前缀)。这个前缀会让你有些悲伤。 https://sqlperformance.com/2012/10/t-sql-queries/sp_prefix

答案 1 :(得分:1)

你可能想考虑给你的@ClientCount一个除NULL以外的默认值,或者在运行其余部分之前重写你的查询以查看@ClientCount是否为null,因为它总是为0。

CREATE TABLE keldan.portfolio_users
(
  username text NOT NULL,
  date_added date NOT NULL,
  CONSTRAINT users_pk PRIMARY KEY (username)
)
WITH (
  OIDS=FALSE
);

CREATE TABLE keldan.portfolios
(
  pid serial NOT NULL,
  username text NOT NULL,
  date_added date NOT NULL,
  name text NOT NULL,
  CONSTRAINT portfolios_pk PRIMARY KEY (pid),
  CONSTRAINT portfolios_fk FOREIGN KEY (username)
      REFERENCES keldan.portfolio_users (username) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);