mysql 預存程序高效分頁方法

來源:互聯網
上載者:User

Create procedure [dbo].[PrGs_Nation_Task_GetList]
@PageSize int = 100, -- 每頁顯示記錄條數,預設為100
@PageIndex int = 1, -- 當前提取要顯示的頁碼,預設為1,資料庫根據PageSize,PageIndex 計算返回一頁資料
@RetTotal int output, -- 記錄總數
@RetCount int output, -- 返回記錄數
@RetPageIndex int output, -- 輸出當前頁碼
@ReturnDesc varchar(128) output -- 返回操作結果描述
as
begin
set nocount on
set xact_abort on
set @RetTotal = 0
set @RetCount = 0
set @RetPageIndex = @PageIndex


-- 多條件取值
declare @Err int -- 錯誤
declare @PageCount int -- 總頁數
declare @BeginRID int -- 開始行 Rid
declare @MaxRow int -- 最後行
select @RetTotal = count(*)
from NationTask
select @Err = @@ERROR
if @Err <> 0
begin
set @ReturnDesc = '提取國家任務總數失敗!'
return -1
end
-- 如果無資料, 則返回空結果集
if @RetTotal = 0
begin
set @ReturnDesc = '當前條件無國家任務記錄!'
return 1
end
-- 計算總頁數
set @PageCount = @RetTotal / @PageSize
if @RetTotal % @PageSize > 0
begin
set @PageCount = @PageCount + 1
end
-- 超過總頁數,則返回空結果集
if @PageIndex > @PageCount
begin
set @ReturnDesc = '當前條件無國家任務記錄!'
return 1
end
-- 擷取 要返回頁面的 第一行紀錄的 Rid
set @MaxRow = @PageSize * (@PageIndex - 1) + 1
set rowcount @MaxRow
select @BeginRID = Rid
from NationTask
order by Rid desc

-- 返回資料列表
set rowcount @PageSize
select Rid
,TaskName
,TaskTitle
,ImageID
,EffectID
,StartTime
from NationTask
where Rid <= @BeginRID
order by Rid desc
set @RetCount = @@rowcount
-- 結束
set @ReturnDesc = '提取國家工作清單成功!'
return 1
end

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.