SQLServer2005 row_number() 分页存储过程

来源:互联网 发布:数据库规范化理论 作用 编辑:程序博客网 时间:2024/04/29 19:58
create procedure [dbo].[GetProductByCategoryId](    @CategoryID int,    @PageIndex int = 0,    @PageSize int = 20,    @TotalRecords int output)asbegin   declare @ResultTable table(    RowIndex int,    ProductID int,    ProductName nvarchar(50),    CategoryID int,    Unit nvarchar(10),    UnitPrice money,    Quantity int);   insert into @ResultTableselect row_number() over (order by ProductID asc) as RowIndex,       p.ProductID, p.ProductName, p.CategoryID, p.Unit, p.UnitPrice, p.Quantityfrom   Products as pwhere CategoryID = @CategoryID;     select  @TotalRecords = count(*) from  @ResultTable;   select *from   @ResultTablewhere  RowIndex > (@PageSize * @PageIndex) and RowIndex <= (@PageSize * (@PageIndex+1));   end;
原创粉丝点击