日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用存储过程创建分页

發布時間:2025/3/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用存储过程创建分页 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在實際的開發過程中,經常遇到存儲過程分頁,下面根據實際情況總結的幾種方法:

數據庫名稱:myTest

1、思路:利用select top and select not in 排除例外情況的分頁
use myTest
go
create procedure proc_paged_with_notin?
?(
???? @pageIndex int,? --頁索引
???? @pageSize int??? --每頁記錄數
?)
as
begin
??? set nocount on; --沒有返回值
??? declare @sql nvarchar(500)
??? set @sql='select top '+str(@pageSize)+' * from tb_TestTable where(ID not in(select top '+str(@pageSize*@pageIndex)+' id from tb_TestTable order by ID ASC)) order by ID'
??? execute(@sql)? --因select top后不支持直接接參數,所以寫成了字符串@sql
??? set nocount off;
end

exec proc_paged_with_notin 10,5

2、思路:利用select top and select max(列)分頁
use myTest
go
create procedure proc_paged_with_selectMax?
?(
???? @pageIndex int,? --頁索引
???? @pageSize int??? --每頁記錄數
?)
?as
?begin
?set nocount on;
??? declare @sql nvarchar(500)?
????if @pageIndex=0
??????????? set @sql='select top '+str(@pageSize)+' * from?tb_TestTable?order by ID'
??? else
????????????set @sql='select top '+str(@pageSize)+' * From?tb_TestTable where(ID>(select max(id) From (select top '+str(@pageSize*@pageIndex)+' id? From ?tb_TestTable?order by ID)?as TempTable)) order by ID'
execute(@sql)
set nocount off;
end

exec proc_paged_with_selectMax 10,5


3、思路:利用Row_number()給數據行加上索引
create procedure proc_paged_with_Rownumber? --利用SQL 2005中的Row_number()
?(
???? @pageIndex int,
???? @pageSize int
?)
as
?begin
?set nocount on;
??? select * from (select *,Row_number() over(order by ID asc) as IDRank from tb_testTable) as IDWithRowNumber where IDRank>@pageSize*@pageIndex and IDRank<@pageSize*(@pageIndex+1)
set nocount off;
end

exec proc_paged_with_Rownumber 10,5

三種方法的測試結果顯示:select max >row_number>not in(分頁速度)

轉載于:https://www.cnblogs.com/jsping/archive/2012/08/19/2646237.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的使用存储过程创建分页的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。