[oracle]pl/sql --分页过程demo
2024-09-04 06:06:14
这句sql能够用来查询一张表中的特定位置的记录
--查询的方法获取分页的语句
select *from
(select t1.*,rownum rn from (select *from books) t1 where rownum<=4)
where rn >2;
--使用分页写存储过程
--1 首先要创建一个包,定义游标类型
create or replace package fenyepackage as
type fenye_cursor is ref cursor;
end fenyepackage;
--2 基本的过程
create or replace procedure fenye(
tablename in varchar2, --表名
pagesize in number, --每页有多大
pagenow in number, --当前页
myrows out number, --总共多少记录
mypageconnt out number, --总共多少页
l_cursor out fenyepackage.fenye_cursor --游标
) is
--得到记录的開始和结束的位置
v_begin number:=(pagenow-1)*pagesize+1;
v_end number:=pagenow*pagesize;
v_sql varchar(1000);
begin
v_sql:='select bookid,bookname,pubhouse from (select t1.*,rownum rn from (select *from '||tablename||') t1 where rownum<='||v_end||')where rn >='||v_begin;
open l_cursor for v_sql;
v_sql:='select count(*) from '||tablename;
execute immediate v_sql into myrows;
if mod(myrows,pagesize)=0 then
mypageconnt:=myrows/pagesize;
else
mypageconnt:=myrows/pagesize+1;
end if;
--close l_cursor;
end;
--3 使用procedure測试
create or replace procedure fenyeceshi(
tablename in varchar2,pagesize in number,pagenow in number) is
v_myrows number;
v_mypagecount number;
v_bookid books.bookid%type;
v_bookname books.bookname%type;
v_pubhouse books.pubhouse%type;
l_cursor fenyepackage.fenye_cursor;
begin
fenye(tablename,pagesize,pagenow,v_myrows,v_mypagecount,l_cursor);
--使用循环读出特定页的记录
loop
fetch l_cursor into v_bookid,v_bookname,v_pubhouse;
--not found要连在一起写
exit when l_cursor%notfound;
dbms_output.put_line('图书ID是:'||v_bookid||' 名称是:'||v_bookname||' 出版社是'||v_pubhouse);
end loop;
dbms_output.put_line('总的记录为:'||v_myrows||'条');
dbms_output.put_line('总的页数为:'||v_mypagecount||'页');
end;
SQL> exec fenyeceshi('books',2,2); 图书ID是:3 名称是:angle 出版社是20
图书ID是:4 名称是:anglele 出版社是10
总的记录为:4条
总的页数为:2页 PL/SQL procedure successfully completed
??: 这样是能得到结果,可是主过程中对于游标是否关闭非常疑惑 没有关闭的情况下是正常的,关闭了的话会出错 这个问题,待后面解决?
SQL> exec fenyeceshi('books',2,2); begin fenyeceshi('books',2,2); end; ORA-01001: 无效的游标 ORA-06512: 在 "SCOTT.FENYECESHI", line 12 ORA-06512: 在 line 2
本文出自 “orangleliu笔记本” 博客,请务必保留此出处http://blog.csdn.net/orangleliu/article/details/38309427
最新文章
- Oracle 正则表达式函数-REGEXP_INSTR 使用例子
- a标签创建超链接,利用a标签创建锚点
- 剑指offer题目31-40
- ContactsContract中涉及数据库中的一些列属性值【Written By KillerLegend】
- css实现居中
- 给jdk写注释系列之jdk1.6容器(10)-Stack&;Vector源码解析
- iOS 7 - Auto Layout on iOS Versions prior to 6.0
- IO调度器原理介绍
- tensorflow dropout函数应用
- 基于FFMPEG的跨平台播放器实现(二)
- 【Python】 Selenium 模拟浏览器 寻路
- mysql六种日志
- asp.net mvc5 安装
- numpy 中clip函数的使用
- 【Android】11.1 Activity的生命周期和管理
- es6 class 了解
- shiro学习笔记_0300_jdbcRealm和认证策略
- MySQL数据约束
- 【第一周】c++实现词频统计
- [BZOJ4456] [Zjoi2016]旅行者 分治+最短路
热门文章
- BZOJ4033 [HAOI2015]树上染色 【树形dp】
- (转)关于Jackson2.x中com.fasterxml.jackson包的用法
- 20深入理解C指针之---程序的栈和堆
- linux内核之进程的基本概念(进程,进程组,会话关系)
- svn不是内部或外部命令?
- java mail Received fatal alert: handshake_failure java 无法发送邮件问题 java 发送qq邮件(含源码)
- Vue开发之路由进阶
- IP分段小记
- python装饰器的深度探究
- 第4章 CentOS软件安装