我有这个程序用于获取当前页面上的项目。我还希望包含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
这是我当前的(剥离的)查询,我怎样才能获得内部选择的最后一个rownumber / total rowcount,或者包括@start
和{{之间的行旁边的最后一行1}}。
答案 0 :(得分: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
)