本篇文章為大家展示了SQLServer分頁存儲過程有哪些,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
創(chuàng)新互聯(lián)主要從事成都網(wǎng)站設(shè)計、做網(wǎng)站、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)自貢,10余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
SQLServer分頁存儲過程有哪幾種方法
創(chuàng)建數(shù)據(jù)庫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耗時setnocountoff;end
SQLServer分頁存儲過程有哪幾種方法
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耗時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耗時setnocountoff;end
5、利用臨時表及Row_number createprocedureproc_CTE--利用臨時表及Row_number(@pageIndexint,--頁索引@pageSizeint--頁記錄數(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分頁存儲過程有哪些,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。