DB2中通用的預存程序分頁程式

來源:互聯網
上載者:User

CREATE PROCEDURE SALES.DB2PAGINATION(IN ITBNAME VARCHAR(2000),-- 表名
     IN ISHOWFIELD VARCHAR(1000),-- 顯示欄位
     IN IJOIN VARCHAR(1000),-- 聯結條件(如:內聯、外聯)
     IN IWHERE VARCHAR(2000),-- 查詢條件 (注意: 不要加 WHERE)
     IN IORDER VARCHAR(100),-- 排序條件 (注意: 不要加 ORDER BY)
     IN IPAGESIZE INTEGER,-- 頁尺寸 如果為0 預設返回前一百萬條資料可以認為是返回所有資料
     INOUT IOCURRENTPAGEIX INTEGER,-- 輸入和輸出:當前頁
     OUT OPAGESTARTROW INTEGER,-- 輸出:當前開始行
     OUT OPAGEENDROW INTEGER,-- 輸出:當前結束行
     OUT OTOTALROWS INTEGER,-- 輸出:當前總記錄數
     OUT OHASPREVIOUSPAGE INTEGER,-- 輸出:是否有上一頁
     OUT OHASNEXTPAGE INTEGER,-- 輸出:是否有下一頁
     OUT OTOTALPAGES INTEGER,-- 輸出:總頁數
     OUT OERROR VARCHAR(100))-- 輸出:錯誤資訊
  RESULT SETS 1
  MODIFIES SQL DATA
  NOT DETERMINISTIC
  LANGUAGE SQL
BEGIN
/**//*----------------------------------------------------------------
* Copyright (C) 2007 Huacius
* 著作權。
*
* 預存程序分頁
*
*
//-----------------------------------------------------------------------*/
DECLARE STRSQL  VARCHAR(6000);-- 主語句

DECLARE result CURSOR WITH RETURN TO CALLER FOR S2;

DECLARE exit handler FOR sqlexception-- 異常捕獲
BEGIN
set OERROR = 'error!';
END;

-- BODY start --
if(iwhere <> '') then
set iwhere = ' where ' || iwhere;
end if;
if(iorder <> '') then
set iorder = 'order by ' || iorder;
end if;
if(ijoin <> '') then
set ijoin = ' ' || ijoin;
end if;

set strsql = 'select count(*) from ' || itbname || ijoin || iwhere;
prepare s2 from strsql;
open result;
  fetch result into ototalrows;-- 總記錄數
  close result;

if(ipagesize = 0) then
set ipagesize = 1000000;-- 每頁顯示數
end if;

set ototalpages = (ototalrows - 1) / ipagesize + 1;-- 總頁數

if(iocurrentpageix < 1) then
set iocurrentpageix = 1;-- 當前頁
else
if(iocurrentpageix > ototalpages) then
set iocurrentpageix = ototalpages;
end if;
end if;

set opagestartrow = ipagesize * (iocurrentpageix -1) + 1;-- 每頁開始數
if(iocurrentpageix = ototalpages) then
set opageendrow = ototalrows;-- 每頁結束數
else
set opageendrow = ipagesize * iocurrentpageix;
end if;

if(iocurrentpageix > 1) then
set ohaspreviouspage = 1;-- 是否有上一頁
else
set ohaspreviouspage = 0;
end if;

if(iocurrentpageix < ototalpages) then
set ohasnextpage = 1;-- 是否有下一頁
else
set ohasnextpage = 0;
end if;

set strsql = 'select * from (select rownumber() over(' || iorder || ') as rownum,'
|| ishowfield
|| ' from '
|| itbname
|| ijoin
|| iwhere
|| ') as temp where rownum between ' || rtrim(char(opagestartrow)) || ' and ' || rtrim(char(opageendrow));
prepare s2 from strsql;
open result;
-- BODY end --

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.