SQL2005如何进行分页??

昨天需要在存储过程中进行分页的操作,以前没在SQL2005中进行分页过,据说2005含有分页的功能,网上查了一下资料,现在整理了一下,以便下次遇到时进行查阅!

create PROCEDURE [dbo].[Pro_Test]
    
-- Add the parameters for the stored procedure here
    @PageSize FLOAT,--每页显示的个数
    @TargetPage SMALLINT--目标页的索引值
AS
BEGIN
    
-- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    
-- Insert statements for procedure here
    WITH Sales_CTE(PageNumber,ID,Numb,Password,CreateDate)
    
AS(
    
select CEILING((ROW_NUMBER() OVER (ORDER By CreateDate desc))/@PageSizeas PageNumber,ID,
    Numb,Password,CreateDate 
from tbl_Wealth_WorthCard 
    )
    
select ID,Numb,Password,CreateDate from Sales_CTE
    
where PageNumber=@TargetPage

对以上存储过程进行分析一下:
with:  这条语句会调用SQL Server中的一个新属性,我们称之为common table expression(CTE),从本质上来说,我们可以将CTE看作是高版本的临时表。
分页的实质就是CTE中的TSQL语句。在下面的选择语句中,我使用了一个新的排序函数——ROW_NUMBER(这一函数很容易使用,你只需要给 ROW_NUMBER函数提供一个域名作为参数,ROW_NUMBER会用它来进行分页)。随后,我使用@PageSize参数来划分每页的行数以及每页的最大行数值。
posted @ 2008-06-27 08:19  单车骑客  阅读(379)  评论(0编辑  收藏  举报