获取存储过程返回的记录数

Get number of records returned by the stored procedure

我有一个存储过程,它按页面大小 10 执行自定义分页,并通过名为 Users 的 table。现在我想让存储过程也 return 由存储过程 return 编辑的总记录数,而不仅仅是页面大小 10.

程序如下:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[zsp_GetUsersAdministratorPanel]
    (@StartRowIndex INT,
     @MaxRows INT,
     @OrderByField NVARCHAR(200),
     @Asc BIT,
     @SearchValue NVARCHAR(200) = NULL)
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @UsersTable TABLE(UserId int,
                              FirstName nvarchar(400),
                              LastName nvarchar(400),
                              Email nvarchar(200)
                             )  

    DECLARE @WhereQuery nvarchar(max)

    SET @WhereQuery = ' WHERE '

    IF LEN(@SearchValue) > 0
        BEGIN
                SET @WhereQuery = @WhereQuery +
                  'FirstName like ''%' + @SearchValue + '%''  or '
                + ' LastName like ''%' + @SearchValue + '%''  or '
                + ' Email like ''%' + @SearchValue + '%'''
        END
    ELSE
    BEGIN 
    set @WhereQuery = ' '
    END
    DECLARE @OrderQuery nvarchar(max)

    IF LEN(@OrderByField) > 0
        BEGIN
            IF @OrderByField = 'Email'
                SET @OrderQuery = ' ORDER BY Email ' +
                    CASE @Asc WHEN 1 
                    THEN ' ASC ' ELSE ' DESC ' END
            ELSE IF @OrderByField = 'FirstName'
                SET @OrderQuery =
                    ' ORDER BY FirstName ' +  
                    CASE @Asc WHEN 1 
                    THEN ' ASC ' ELSE ' DESC '  END
            ELSE IF @OrderByField = 'LastName'  
                SET @OrderQuery = ' ORDER BY LastName ' +  
                    CASE @Asc WHEN 1 
                    THEN ' ASC ' ELSE ' DESC '  END     

        END 

    DECLARE @Query  NVARCHAR(MAX)
    SET @Query = 'select UserId, 
                FirstName,
                LastName,
                Email,
                @@ROWCOUNT as TotalRows
            from 
            (


                SELECT  ROW_NUMBER() OVER ('+ @OrderQuery +')
                    AS [ROW_NUMBER], 
                        [t0].[UserId], [t0].[FirstName], 
                        [t0].[LastName], 
                        [t0].[Email]
                        FROM [dbo].[Users] AS [t0]' +
                        @WhereQuery +
            ') AS [t1]' +
                 + ' WHERE [ROW_NUMBER] BETWEEN 
                    @StartRowIndex AND @MaxRows' + @OrderQuery

    PRINT @Query

    INSERT INTO @UsersTable EXEC sp_Executesql @Query,
                    N'@OrderQuery nvarchar(max),
                    @StartRowIndex int,@MaxRows int',
                    @OrderQuery=@OrderQuery,
                    @StartRowIndex = @StartRowIndex,
                    @MaxRows = @MaxRows

    SELECT * FROM @UsersTable           

    SET NOCOUNT OFF;
END

我怎样才能做到这一点,有人可以帮助我吗? :)

我想我必须使用 @@RowCount,但我不太确定在哪里?

P.S。伙计们,我的意思是我得到了 return 由 "Where" 语句编辑的结果,而不仅仅是页面大小??

P.S。伙计们,如果您有任何改进程序执行时间的建议,请这样做:)

将行数列添加到您的 table 变量:

DECLARE @UsersTable TABLE(TotalRows int, UserId int, ...

您可以将生成的 SQL 修改为:

SET @Query = '
    SELECT  TotalRows, UserId, ...
    FROM    (
            SELECT  ROW_NUMBER() OVER ('+ @OrderQuery +') AS RowNum
            ,       COUNT(*) OVER () AS TotalRows
            ,       ...
            ) AS t1
    WHERE   RowNum BETWEEN ...

现在您的程序将 return 第一列中的总行数。

我在想这样的事情:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[zsp_GetUsersAdministratorPanel]
    (@StartRowIndex INT,
     @MaxRows INT,
     @OrderByField NVARCHAR(200),
     @Asc BIT,
     @SearchValue NVARCHAR(200) = NULL)
AS
BEGIN
    SET NOCOUNT ON;
    declare @counter int = 0
    DECLARE @UsersTable TABLE(UserId int,
                              FirstName nvarchar(400),
                              LastName nvarchar(400),
                              Email nvarchar(200)
                             )  

    DECLARE @WhereQuery nvarchar(max)

    SET @WhereQuery = ' WHERE '

    IF LEN(@SearchValue) > 0
        BEGIN
                SET @WhereQuery = @WhereQuery +
                  'FirstName like ''%' + @SearchValue + '%''  or '
                + ' LastName like ''%' + @SearchValue + '%''  or '
                + ' Email like ''%' + @SearchValue + '%'''
        END
    ELSE
    BEGIN 
    set @WhereQuery = ' '
    END
    DECLARE @OrderQuery nvarchar(max)

    IF LEN(@OrderByField) > 0
        BEGIN
            IF @OrderByField = 'Email'
                SET @OrderQuery = ' ORDER BY Email ' +
                    CASE @Asc WHEN 1 
                    THEN ' ASC ' ELSE ' DESC ' END
            ELSE IF @OrderByField = 'FirstName'
                SET @OrderQuery =
                    ' ORDER BY FirstName ' +  
                    CASE @Asc WHEN 1 
                    THEN ' ASC ' ELSE ' DESC '  END
            ELSE IF @OrderByField = 'LastName'  
                SET @OrderQuery = ' ORDER BY LastName ' +  
                    CASE @Asc WHEN 1 
                    THEN ' ASC ' ELSE ' DESC '  END     

        END 

    DECLARE @Query  NVARCHAR(MAX)
    SET @Query = 'select UserId, 
                FirstName,
                LastName,
                Email,
                @@ROWCOUNT as TotalRows
            from 
            (


                SELECT  ROW_NUMBER() OVER ('+ @OrderQuery +')
                    AS [ROW_NUMBER], 
                        [t0].[UserId], [t0].[FirstName], 
                        [t0].[LastName], 
                        [t0].[Email]
                        FROM [dbo].[Users] AS [t0]' +
                        @WhereQuery +
            ') AS [t1]' +
                 + ' WHERE [ROW_NUMBER] BETWEEN 
                    @StartRowIndex AND @MaxRows' + @OrderQuery

    PRINT @Query

    INSERT INTO @UsersTable EXEC sp_Executesql @Query,
                    N'@OrderQuery nvarchar(max),
                    @StartRowIndex int,@MaxRows int',
                    @OrderQuery=@OrderQuery,
                    @StartRowIndex = @StartRowIndex,
                    @MaxRows = @MaxRows

select @counter=@counter+@@rowcount
select @counter

    SELECT * FROM @UsersTable           

    SET NOCOUNT OFF;
END