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

4

1 回答 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
    )
于 2010-09-16T10:02:08.407 回答