分页 存储过程

    技术2022-05-11  59

    方法一(任何条件的SQL语句'):

    create  procedure XiaoZhengGe@sqlstr nvarchar(4000), --查询字符串@currentpage int,--第N页@pagesize int--每页行数asset nocount ondeclare @P1 int,--P1是游标的id@rowcount intexec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount outputselect ceiling(1.0*@rowcount/@pagesize) as 总页数--,@rowcount as 总行数,@currentpage as 当前页set @currentpage=(@currentpage-1)*@pagesize+1exec sp_cursorfetch @P1,16,@currentpage,@pagesize exec sp_cursorclose @P1set nocount off

    测试: XiaoZhengGe '任何条件的SQL语句',2,10

    方法二(前面的页面记取快,越往后读越慢):

    lihonggen0(李洪根,MS MVP)

    假若你用几十万行数据,你可以看看.

    你的存储过程还比不上NOT IN分页,示例:SELECT Top 10 * FROM Customers WHERE Customerid NOT IN (SELECT TOP 20 Customerid from Customers )

    declare @SQLStr varchar(8000)set @SQLStr='SELECT Top '+cast(@每页大小 as varchar)+' * FROM 表 WHERE 主键列 NOT IN (SELECT TOP '+cast(@每页大小*@第几页 as varchar)+' 主键列 from 表 )'exec(@SQLStr)

    我个人认为最好的分页方法是:Select top 10 * from table where id>200写成存储过程,上面的语句要拼一下sql语句,要获得最后大于的哪一个ID号  


    最新回复(0)