您好,登錄后才能下訂單哦!
本篇文章為大家展示了SQLServer分頁存儲過程有哪些,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
SQLServer分頁存儲過程有哪幾種方法
創建數據庫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 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()給數據行加上索引
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(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
上述內容就是SQLServer分頁存儲過程有哪些,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。