频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

GridView自定义分页的四种存储过程

来源:blog.csdn.net 作者:高歌 出处:巧巧读书 2008-07-16 进入讨论组
下一页 1 2 3 

        今天有空总结了一下使用存储过程对GridView进行分页的4种写法(分别是使用Top关键字,临时表,临时表变量和SQL Server 2005 新加的Row_Number()函数)

         if exists(select 1 from sys.objects where name = 'GetProductsCount' and type = 'P')
         drop proc GetProductsCount
        go
        CREATE PROCEDURE GetProductsCount
        as
         select count(*) from products
        go

        --1.使用Top

         if exists(select 1 from sys.objects where name = 'GetProductsByPage' and type = 'P')
         drop proc GetProductsByPage
        go
        CREATE PROCEDURE GetProductsByPage
         @PageNumber int,
         @PageSize int
        AS
         declare @sql nvarchar(4000)
         set @sql = 'select top ' + Convert(varchar, @PageSize)
          + ' * from products where productid not in (select top ' + Convert(varchar, (@PageNumber - 1) * @PageSize)  + ' productid from products)'
         exec sp_executesql @sql
        go
        --exec GetProductsByPage 1, 10
        --exec GetProductsByPage 5, 10

        --2.使用临时表

         if exists(select 1 from sys.objects where name = 'GetProductsByPage' and type = 'P')
         drop proc GetProductsByPage
        go
        CREATE PROCEDURE GetProductsByPage
         @PageNumber int,
         @PageSize int
        AS

        -- 创建临时表

         CREATE TABLE #TempProducts
        (
         ID int IDENTITY PRIMARY KEY,
         ProductID int,
         ProductName varchar(40) ,
         SupplierID int,
         CategoryID int,
         QuantityPerUnit nvarchar(20),
         UnitPrice money,
         UnitsInStock smallint,
         UnitsOnOrder smallint,
         ReorderLevel smallint,
         Discontinued bit
        )

        -- 填充临时表

         INSERT INTO #TempProducts
        (ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued)
        SELECT ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued
        FROM Products
        DECLARE @FromID int
        DECLARE @ToID int
        SET @FromID = ((@PageNumber - 1) * @PageSize) + 1
        SET @ToID = @PageNumber * @PageSize
        SELECT ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued
        FROM #TempProducts
        WHERE ID >= @FromID AND ID <= @ToID
        go
        --exec GetProductsByPage 1, 10
        --exec GetProductsByPage 5, 10

        --3.使用表变量

        /*

        为要分页的数据创建一个table变量,这个table变量里有一个作为主健的IDENTITY列.这样需要分页的每条记录在table变量里就和一个row index(通过IDENTITY列)关联起来了.一旦table变量产生,连接数据库表的SELECT语句就被执行,获取需要的记录.SET ROWCOUNT用来限制放到table变量里的记录的数量.

        当SET ROWCOUNT的值指定为PageNumber * PageSize时,这个方法的效率取决于被请求的页数.对于比较前面的页来说– 比如开始几页的数据– 这种方法非常有效. 但是对接近尾部的页来说,这种方法的效率和默认分页时差不多

        */

         if exists(select 1 from sys.objects where name = 'GetProductsByPage' and type = 'P')
         drop proc GetProductsByPage
        go
        CREATE PROCEDURE GetProductsByPage
         @PageNumber int,
         @PageSize int
        AS
        DECLARE @TempProducts TABLE
        (
           ID int IDENTITY,
           productid int
        )
        DECLARE @maxRows int
        SET @maxRows = @PageNumber * @PageSize

        --在返回指定的行数之后停止处理查询

         SET ROWCOUNT @maxRows
        INSERT INTO @TempProducts (productid)
        SELECT productid
        FROM products
        ORDER BY productid
        SET ROWCOUNT @PageSize
        SELECT p.*
        FROM @TempProducts t INNER JOIN products p
        ON t.productid = p.productid
        WHERE ID > (@PageNumber - 1) * @PageSize
        SET ROWCOUNT 0
        GO
        --exec GetProductsByPage 1, 10
        --exec GetProductsByPage 5, 10

更多文章 更多内容请看存储过程网络存储—光纤通道服务器存储专栏专题,或进入讨论组讨论。
下一页 1 2 3 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章