本篇文章為大家展示了SQLServer分頁(yè)存儲(chǔ)過(guò)程有哪些,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
在保山等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì) 網(wǎng)站設(shè)計(jì)制作定制網(wǎng)站開(kāi)發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),成都全網(wǎng)營(yíng)銷,外貿(mào)網(wǎng)站制作,保山網(wǎng)站建設(shè)費(fèi)用合理。SQLServer分頁(yè)存儲(chǔ)過(guò)程有哪幾種方法
創(chuàng)建數(shù)據(jù)庫(kù)data_Test:
createdatabasedata_TestGOusedata_TestGOcreatetabletb_TestTable--創(chuàng)建表(idintidentity(1,1)primarykey,userNamenvarchar(20)notnull,userPWDnvarchar(20)notnull,userEmailnvarchar(40)null)GO
插入數(shù)據(jù):
setidentity_inserttb_TestTableondeclare@countintset@count=1while@count<=2000000begininsertintotb_testtable(id,username,userpwd,useremail)values(@count,'admin','admin888','lli0077@yahoo.com.cn count="@count+1endsetidentity_inserttb_TestTableoff" 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耗時(shí)setnocountoff;end
SQLServer分頁(yè)存儲(chǔ)過(guò)程有哪幾種方法
3、利用selecttop和中間變量
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耗時(shí)setnocountoff;end
4、利用Row_number()此方法為SQLserver2005中新的方法,利用Row_number()給數(shù)據(jù)行加上索引
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耗時(shí)setnocountoff;end
5、利用臨時(shí)表及Row_number createprocedureproc_CTE--利用臨時(shí)表及Row_number(@pageIndexint,--頁(yè)索引@pageSizeint--頁(yè)記錄數(shù))assetnocounton;declare@ctestrnvarchar(400)declare@strSqlnvarchar(400)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
上述內(nèi)容就是SQLServer分頁(yè)存儲(chǔ)過(guò)程有哪些,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。