我想要一个存储过程为y个页面取x个数据行。 例如
我有20个数据流 我的页面大小是2 如果我选择第2页 我将获得数据行17,18
我可以使用前200并使用order by来选择第一个和最后一个datarow但是我如何获取它们之间的页面。
@PageNumber INT
As
BEGIN
SELECT COUNT(rate.RateID)/200 FROM dbo.Rate where dbo.Rate.Hourly =0
DECLARE @LastIndex INT
SET @LastIndex= (SELECT TOP 1 rate.RateID FROM dbo.Rate where dbo.Rate.Hourly =0 ORDER BY rate.RateID ASC)
Select TOP 200
[RateID],
[PairID],
[Open],
[Close],
[High],
[Low],
[Difference],
[Average],
[Percentage],
[InfoDate],
[Hourly],
[CaptureDateTime]
From Rate
WHERE Hourly =0 AND RateID >=(@LastIndex+(200* @PageNumber))
ORDER BY [RateID] ASC
结束 这就是我现在所拥有的,但它不能正常工作
答案 0 :(得分:6)
好的,既然你没有指定你正在使用的RDBMS,我可以给你一个至少对SQL Server 2005 +有效的解决方案。
DECLARE @PageNumber INT, @PageSize INT
SET @PageNumber = 3
SET @PageSize = 5;
WITH CTE AS
(
SELECT *, ROW_NUMBER() OVER(ORDER BY RateID) Corr
FROM Rate
WHERE Hourly = 0
)
SELECT *
FROM CTE
WHERE Corr BETWEEN @PageNumber*@PageSize AND @PageNumber*@PageSize+@PageSize-1
此外,您应该知道在下一版本的SQL Server(“Denali”)中,通过对TOP
子句进行一些修改,这将更加容易。
答案 1 :(得分:0)
Select * from(
SELECT
(ROW_NUMBER()OVER (ORDER BY InfoDate ASC)) AS RowNo,
[RateID],
[PairID],
[Open],
[Close],
[High],
[Low],
[Difference],
[Average],
[Percentage],
[InfoDate],
[Hourly],
[CaptureDateTime]
From Rate
) AS T
WHERE t.RowNo
BETWEEN 200*@PageNumber AND 200 * (@PageNumber+1)-1
ORDER BY RowNo DESC
这就是我用过的......