本篇文章给大家分享的是有关SQL Server中怎么实现分页存储,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创建数据库data_Test :

createdatabasedata_TestGOusedata_TestGOcreatetabletb_TestTable--创建表(idintidentity(1,1)primarykey,userNamenvarchar(20)notnull,userPWDnvarchar(20)notnull,userEmailnvarchar(40)null)GO

插入数据:

setidentity_inserttb_TestTableondeclare@countintset@count=1while@count<=2000000begininsertintotb_TestTable(id,userName,userPWD,userEmail)values(@count,'admin','admin888','lli0077@yahoo.com.cn')set@count=@count+1endsetidentity_inserttb_TestTableoff

1、利用select top 和select not in进行分页

具体代码如下:

createprocedureproc_paged_with_notin--利用selecttopandselectnotin(@pageIndexint,--页索引@pageSizeint--每页记录数)asbeginsetnocounton;declare@timediffdatetime--耗时declare@sqlnvarchar(500)select@timediff=Getdate()set@sql='selecttop'+str(@pageSize)+'*fromtb_TestTablewhere(IDnotin(selecttop'+str(@pageSize*@pageIndex)+'idfromtb_TestTableorderbyIDASC))orderbyID'execute(@sql)--因selecttop后不支技直接接参数,所以写成了字符串@sqlselectdatediff(ms,@timediff,GetDate())as耗时setnocountoff;end

2、利用select top 和 select max(列键)

createprocedureproc_paged_with_selectMax--利用selecttopandselectmax(列)(@pageIndexint,--页索引@pageSizeint--页记录数)asbeginsetnocounton;declare@timediffdatetimedeclare@sqlnvarchar(500)select@timediff=Getdate()set@sql='selecttop'+str(@pageSize)+'*Fromtb_TestTablewhere(ID>(selectmax(id)From(selecttop'+str(@pageSize*@pageIndex)+'idFromtb_TestTableorderbyID)asTempTable))orderbyID'execute(@sql)selectdatediff(ms,@timediff,GetDate())as耗时setnocountoff;end

3、利用select top和中间变量

createprocedureproc_paged_with_Midvar--利用ID>最大ID值和中间变量(@pageIndexint,@pageSizeint)asdeclare@countintdeclare@IDintdeclare@timediffdatetimedeclare@sqlnvarchar(500)beginsetnocounton;select@count=0,@ID=0,@timediff=getdate()select@count=@count+1,@ID=casewhen@count<=@pageSize*@pageIndexthenIDelse@IDendfromtb_testTableorderbyidset@sql='selecttop'+str(@pageSize)+'*fromtb_testTablewhereID>'+str(@ID)execute(@sql)selectdatediff(ms,@timediff,getdate())as耗时setnocountoff;end

4、利用Row_number() 此方法为SQL server 2005中新的方法,利用Row_number()给数据行加上索引

createprocedureproc_paged_with_Rownumber--利用SQL2005中的Row_number()(@pageIndexint,@pageSizeint)asdeclare@timediffdatetimebeginsetnocounton;select@timediff=getdate()select*from(select*,Row_number()over(orderbyIDasc)asIDRankfromtb_testTable)asIDWithRowNumberwhereIDRank>@pageSize*@pageIndexandIDRank<@pageSize*(@pageIndex+1)selectdatediff(ms,@timediff,getdate())as耗时setnocountoff;end

5、利用临时表及Row_number

createprocedureproc_CTE--利用临时表及Row_number(@pageIndexint,--页索引@pageSizeint--页记录数)assetnocounton;declare@ctestrnvarchar()declare@strSqlnvarchar()declare@datediffdatetimebeginselect@datediff=GetDate()set@ctestr='withTable_CTEas(selectceiling((Row_number()over(orderbyIDASC))/'+str(@pageSize)+')aspage_num,*fromtb_TestTable)';set@strSql=@ctestr+'select*FromTable_CTEwherepage_num='+str(@pageIndex)endbeginexecutesp_executesql@strSqlselectdatediff(ms,@datediff,GetDate())setnocountoff;end

以上就是SQL Server中怎么实现分页存储,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。