发送总行数/最后一行,包含在结果集中

问题描述 投票:0回答:1

我有这个过程用于获取当前页面上的项目。我还希望包含一个具有项目总数的

OUT
var,以便我可以计算总页数。

USE [some_name]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[GetFilteredProductDetails]
    @start int,
    @end int
AS
BEGIN
    SET NOCOUNT ON;

        SELECT *
        FROM
        (
            SELECT *, (ROW_NUMBER() OVER (ORDER BY itemid)) AS row
            /* the rest of a big complex query that, so far, works.*/
        ) AS q
        WHERE
        (           
            row BETWEEN @start AND @end
        )
END

这是我当前的(剥离的)查询,我如何才能获得内部选择的最后一个行号/总行数,或者包括

@start
@end
之间的行旁边的最后一行。

sql sql-server-2005 pagination rowcount
1个回答
2
投票
带有空

COUNT(*)

 子句的 
OVER()
将为您提供总行数。如果您需要返回最后一行,您可以将其添加到
WHERE
子句中。

SELECT *
FROM
(
    SELECT *, 
    (ROW_NUMBER() OVER (ORDER BY itemid)) AS row,
    COUNT(*) OVER() AS row_count
    /* the rest of a big complex query that, so far, works.*/
) AS q
 WHERE
    (           
        row BETWEEN @start AND @end or row=row_count
    )
© www.soinside.com 2019 - 2024. All rights reserved.