--1
declare
type emp_cursor_type is ref cursor;
emp_cursor emp_cursor_type;
emp_record emp%rowtype;
begin
open emp_cursor for select * from emp where deptno=10;
loop
fetch emp_cursor into emp_record;
exit when emp_cursor%notfound;
dbms_output.put_line('第'||emp_cursor%rowcount||'个雇员'||emp_record.ename);
end loop;
close emp_cursor;
end;
--定义 ref cursor类型时 ,指定return 子句
declare
type emp_record_type is record(
name varchar2(10),
salary number(6,2)
);
type emp_cursor_type is ref cursor return emp_record_type;
emp_cursor emp_cursor_type;
emp_record emp_record_type;
begin
open emp_cursor for select ename,sal from emp;
loop
fetch emp_cursor into emp_record;
exit when emp_cursor%notfound;
dbms_output.put_line('第'||emp_cursor%rowcount||'个雇员的名字:'||emp_record.name);
end loop;
close emp_cursor;
end;
--使用cursor表达式,嵌套cursor
declare
type refcursor is ref cursor;
cursor dept_cursor(no number) is
select a.dname,cursor(select ename,sal from emp where deptno=a.deptno)
from dept a where a.deptno=no;
empcur refcursor;
v_dname dept.dname%type;
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
open dept_cursor(&no);
loop
fetch dept_cursor into v_dname,empcur;
exit when dept_cursor%notfound;
dbms_output.put_line('部门名:'||v_dname);
loop
fetch empcur into v_ename,v_sal;
exit when empcur%notfound;
dbms_output.put_line('雇员名:'||v_ename||',工资:'||v_sal);
end loop;
end loop;
close dept_cursor;
end;
---The STRONG_REF_CURSOR and until Oracle 9i also the weak-type need to be declared in a package structure lik this:
create or replace package refcursor_pkg as
type weak8i_ref_cursor is ref cursor;
type strong_ref_cursor is ref cursor return emp%rowtype;
end refcursor_pkg;
--The pl/sql procedure that returns a ref-cursor looks like this:
create or replace procedure test(
p_deptno in number,
p_cursor out refcursor_pkg.weak8i_ref_cursor
)
is
begin
open p_cursor for
select * from emp where deptno=p_deptno;
end ;
--Since Oracle 9i you can use SYS_REFCURSOR as the type for the returning REF_CURSOR.
create or replace procedure test(
p_deptno in number,
p_cursor out sys_refcursor
)
is
begin
open p_cursor for
select * from emp where deptno=p_deptno;
end ;
/*
Selecting the ref_cursor from JDBC
To get the cursor from Java you can use the following JDBC-code:
*/
public void method() throws SQLException{
Connection conn = getConnection();
CallableStatement cstmt = null;
ResultSet rs = null;
int deptno = 10;
Object temp;
try{
cstmt = conn.prepareCall("begin test(?,?); end;");
cstmt.setInt(1, deptno);
cstmt.registerOutParameter(2, OracleTypes.CURSOR);
cstmt.execute();
rs = (ResultSet) cstmt.getObject(2);
ResultSetMetaData rsm = rs.getMetaData();
int columnCount = rsm.getColumnCount();
while (rs.next()){
for (int j=0;j< columnCount;j++){
temp = rs.getObject(j+1);
}
}
} finally {
if (!rs==null){
rs.close();
}
if (!stmt==null){
stmt.close();
}
if (!conn==null){
conn.close();
}
}
}
--Calling ref-cursor from pl/sql
create or replace procedure test_call is
c_cursor REFCURSOR_PKG.STRONG_REF_CURSOR;
r_emp emp%rowtype;
begin
test(10,c_cursor);
loop
fetch c_cursor into r_emp;
exit when c_cursor%notfound;
dbms_output.put_line(r_emp.ename);
end loop;
close c_cursor;
end test_call;
分享到:
相关推荐
oracle游标变量和数据包
游标是SQL的一个内存工作区,由系统或用户以变量的形式定义。游标的作用就是用于临时存储从数据库中提取的数据块。在某些情况下,需要把数据从存放在磁盘的表中调到计算机内存中进行处理,最后将处理结果显示出来或...
在PL/SQL中可以使用游标处理数据。通过使用游标可以大大提高PL/SQL程序对数据...本章将主要介绍如何使用显式游标进行多行数据的查询、游标FOR循环以及游标变量的使用,另外还将介绍游标属性以及嵌套游标的使用等内容。
Oracle_的存储过程及游标Oracle_的存储过程及游标Oracle_的存储过程及游标Oracle_的存储过程及游标Oracle_的存储过程及游标Oracle_的存储过程及游标Oracle_的存储过程及游标Oracle_的存储过程及游标
本文实例讲述了Oracle存储过程游标用法。分享给大家供大家参考,具体如下: 使用游标的5个步骤 1、声明一些变量用于保存select语句返回的指 2、声明游标,并指定select 语句 3、打开游标 4、从游标中获取记录 5、...
文章目录游标1.1 显式游标1.1.1 创建游标1.1.2 打开游标1.1.3 获取数据1.1.4 关闭游标1.1.5 使用实例1.2 隐式游标1.2.2 游标属性1.3 FOR游标1.4 游标变量1.4 游标表达式1.5 动态游标 游标 游标是一个指针,指向一块...
包含oracle存储过程的增、删、查、改 %type、%rowtype的使用 自定义函数 PL/SQL块
PLSQL语句课件,包括游标游标变量,存储过程,函数,触发器,异常处理等等
Oracle笔记 六、PL/SQL简单语句块、变量定义 Oracle笔记 七、PL/SQL 异常处理 Oracle笔记 八、PL/SQL跳转/判断/循环语句块 Oracle笔记 九、PL/SQL 游标的使用 Oracle笔记 十、PL/SQL存储过程 Oracle笔记 十一、...
从存储过程 自定义函数格式 游标 变量 赋值 语句结束符 大小写 Select 语法 Update语法 Delete语法 动态SQL语句 TOP用法 等各方面对比两个数据库的差异
很好,很实用,嵌入了SQL LOB 类型处理结构 ※ 增加了对标准动态SQL 接口的支持 Pro*C/C++从此版本起具备了执行标准动态SQL 语句的接口,增强了Oracle ...Objects,结构数组,游标变量和LOBS。 ※ DML 语句开始支持返回
Oracle笔记 六、PL/SQL简单语句块、变量定义 Oracle笔记 七、PL/SQL 异常处理 Oracle笔记 八、PL/SQL跳转/判断/循环语句块 Oracle笔记 九、PL/SQL 游标的使用 Oracle笔记 十、PL/SQL存储过程 Oracle笔记 十一、...
第1章 Oracle概述 第2章 管理用户 第3_4章 SQL基础 第5章 SQL中级 第6章 PLSQL 第7章 异常 第8章 游标与存储过程 第9章 游标变量与数据包
这是我学习Oracle时,老师给的讲义,包含了Oracle从入门到高级应用的全部资料。 以下是文件列表 Day01-Oracle基础.pdf ...Day10-变量定义和循环控制.pdf Day11-plsql游标和函数.pdf Day12-触发器.pdf Day13-索引.pdf
4.2.5 游标变量 4.3 过程 4.3.1 创建过程 4.3.2 调用过程 4.3.3 删除过程 4.3.4 过程的参数类型及传递 4.4 函数 4.4.1 创建函数 4.4.2 调用函数 4.4.3 删除函数 4.5 程序包 4.5.1 基本原理 ...
PL/SQL 变量的使用 表达式 PL/SQL结构控制 PL/SQL中使用DML和DDL语言 PL/SQL中的异常 PL/SQL函数编写 第8章-游标,数据的缓存区 什么是游标 显示游标 隐式游标 第9章-视图,数据库中虚拟的表 什么是...
plsql入门 游标变量 Oracle EXTRACT()函数 用Java调用存储过程 ORACLE用户常用数据字典的查询使用方法
8.Oracle数据库游标使用大全 76 其他DML语句 77 DML语句的结果 78 使用游标 79 从游标提取数据 81 记录变量 82 带参数的游标 83 游标FOR循环 84 在游标FOR循环中使用查询 86 游标中的子查询 86 9.PL/SQL异常处理初步...
结合实例,介绍了oracle存储过程的用法,包括定义,变量类型,游标,流程分支语句的使用