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

时间:2018-03-26 20:14:55

标签: sql sql-server sql-server-2008 stored-procedures rowcount

我有一个存储过程,它按页面大小10执行自定义分页,并通过一个名为Users的表。现在我想使存储过程也返回存储过程返回的总记录数,但不仅仅是页面大小为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。伙计们,我的意思是我得到的结果是" Where"声明,而不仅仅是页面大小??

P.S。如果你有任何建议来改善程序的执行时间,请这样做:)

2 个答案:

答案 0 :(得分:1)

将rowcount的列添加到表变量中:

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 ...

现在,您的过程将返回第一列中的总行数。

答案 1 :(得分:1)

我在想这样的事情:

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