分頁儲存過程

2022-08-03 07:27:11 字數 3439 閱讀 3919

--版本1  適用單錶

create or replace procedure soa_getpager_v1(p_pagesize       number, --每頁記錄數

p_pageno         number, --當前頁碼,從 1 開始

p_sqlcols        varchar2, --選擇列

p_sqlfrom        varchar2, --表名or連線查詢

p_sqlwhere       nvarchar2, --查詢子句

p_sqlorderby     varchar2, --排序

p_outrecordcount out number, --返回總記錄數

p_outpagecount   out number, --返回總頁數

p_counts         out sys_refcursor) as

v_sql       varchar2(3000);

p_sqlselect varchar2(3000);

v_count     number;

v_heirownum number;

v_lowrownum number;

v_sqlerror  varchar2(100);

begin

----拼接sql查詢語句

p_sqlselect = 'select '  p_sqlcols  ' from '  p_sqlfrom

' where '  p_sqlwhere;

----取記錄總數

v_sql = 'select count(1) from '  p_sqlfrom  ' where '  p_sqlwhere;

execute immediate v_sql

into v_count;

p_outrecordcount = v_count;

----取總頁數

p_outpagecount = p_outrecordcount  p_pagesize;

if p_outpagecount = 0 then

p_outpagecount = 1;

end if;

p_sqlselect = p_sqlselect  ' order by '  p_sqlorderby;

----執行分頁查詢

v_heirownum = p_pageno  p_pagesize;

v_lowrownum = v_heirownum - p_pagesize + 1;

v_sql = 'select  from (

select a.,rownum rn

from  ( '  p_sqlselect  ') a

where rownum = '  to_char(v_heirownum)  '

) bwhere rn = '  to_char(v_lowrownum);

--注意對rownum別名的使用,第一次直接用rownum,第二次一定要用別名rn

dbms_output.put_line(v_sql);

open p_counts for v_sql;

exception

when others then

v_sqlerror     = sqlerrm;

p_outpagecount = -1;

end soa_getpager_v1;

--版本2 適用多表

create or replace procedure soa_getpager_v2(p_pagesize       number, --每頁記錄數

p_pageno         number, --當前頁碼,從 1 開始

p_sqlcols        varchar2, --選擇列

p_sqlfrom        varchar2, --表名or連線查詢

p_sqlwhere       nvarchar2, --查詢子句

p_sqlorderby     varchar2, --排序

p_outrecordcount out number, --返回總記錄數

p_outpagecount   out number, --返回總頁數

p_counts         out sys_refcursor) as

v_sql       varchar2(3000);

p_sqlselect varchar2(3000);

v_count     number;

v_heirownum number;

v_lowrownum number;

v_sqlerror  varchar2(100);

begin

----拼接sql查詢語句

p_sqlselect := 'select ' || p_sqlcols || ' from ' || p_sqlfrom ||

' where ' || p_sqlwhere;

----取記錄總數

v_sql := 'select count(1) from ' || p_sqlfrom || ' where ' || p_sqlwhere;

execute immediate v_sql

into v_count;

p_outrecordcount := v_count;

----取總頁數

p_outpagecount := p_outrecordcount / p_pagesize;

if p_outpagecount = 0 then

p_outpagecount := 1;

end if;

p_sqlselect := p_sqlselect || ' order by ' || p_sqlorderby;

----執行分頁查詢

v_heirownum := p_pageno * p_pagesize;

v_lowrownum := v_heirownum - p_pagesize + 1;

v_sql := 'select * from (

select a.*,rownum rn

from  ( ' || p_sqlselect || ') a

where rownum <= ' || to_char(v_heirownum) || '

) bwhere rn >= ' || to_char(v_lowrownum);

--注意對rownum別名的使用,第一次直接用rownum,第二次一定要用別名rn

dbms_output.put_line(v_sql);

open p_counts for v_sql;

exception

when others then

v_sqlerror     := sqlerrm;

p_outpagecount := -1;

end soa_getpager_v2;

分頁儲存過程 分頁儲存過程

分頁儲存過程 alter proc dbo p pageshow pagesize int,每頁大小 currentpage int out,當前頁 housename nvarchar 50 房產名稱 totalcount int out,總記錄數 totalpage int out 總頁數 as...

分頁儲存過程

create proc p sobigo percentpage tblname varchar 255 t category 表名 strgetfields varchar 1000 需要返回的列 fldname varchar 255 排序的欄位名 pagesize int 10,頁尺寸 pag...

分頁儲存過程

create procedure pro select pageindex int,pagesize int as select student.sno,student.sname,student.s grade.math,grade.physics,grade.huaxue,grade.chine...