eygle.com   eygle.com
eygle.com  
 

« AIX 5.3系统中如何设置>2G的大文件支持 | Digest首页 | 66首经典的少儿歌曲下载链接 »

How to use the oracle REF CURSOR

链接:
原文地址: http://www.psoug.org/reference/ref_cursors.html
Oracle Ref Cursors
Version 10.2

Strongly Typed
Note: A REF CURSOR that specifies a specific return type.
Package Header CREATE OR REPLACE PACKAGE strongly_typed IS

TYPE return_cur IS REF CURSOR RETURN all_tables%ROWTYPE;
PROCEDURE child(p_return_rec OUT return_cur);
PROCEDURE parent(p_NumRecs PLS_INTEGER);

END strongly_typed;
/
Package Body CREATE OR REPLACE PACKAGE BODY strongly_typed IS
PROCEDURE child(p_return_rec OUT return_cur) IS

BEGIN
OPEN p_return_rec FOR
SELECT * FROM all_tables;
END child;
--==================================================
PROCEDURE parent (p_NumRecs PLS_INTEGER) IS
p_retcur return_cur;
at_rec all_tables%ROWTYPE;
BEGIN
child(p_retcur);

FOR i IN 1 .. p_NumRecs
LOOP
FETCH p_retcur
INTO at_rec;

dbms_output.put_line(at_rec.table_name ||
' - ' || at_rec.tablespace_name ||
' - ' || TO_CHAR(at_rec.initial_extent) ||
' - ' || TO_CHAR(at_rec.next_extent));
END LOOP;
END parent;
END strongly_typed;
/
To Run The Demo set serveroutput on

exec strongly_typed.parent(1)
exec strongly_typed.parent(8)

Weakly Typed
Note: A REF CURSOR that does not specify the return type such as SYS_REFCURSOR.
Child Procedure CREATE OR REPLACE PROCEDURE child (
p_NumRecs IN PLS_INTEGER,
p_return_cur OUT SYS_REFCURSOR)
IS

BEGIN
OPEN p_return_cur FOR
'SELECT * FROM all_tables WHERE rownum <= ' || p_NumRecs ;
END child;
/
Parent Procedure CREATE OR REPLACE PROCEDURE parent (pNumRecs VARCHAR2) IS
p_retcur SYS_REFCURSOR;
at_rec all_tables%ROWTYPE;
BEGIN
child(pNumRecs, p_retcur);

FOR i IN 1 .. pNumRecs
LOOP

FETCH p_retcur
INTO at_rec;

dbms_output.put_line(at_rec.table_name ||
' - ' || at_rec.tablespace_name ||
' - ' || TO_CHAR(at_rec.initial_extent) ||
' - ' || TO_CHAR(at_rec.next_extent));
END LOOP;
END parent;
/
To Run The Demo set serveroutput on

exec parent(1)
exec parent(17)

Passing Ref Cursors
Ref Cursor Passing Demo CREATE TABLE employees (
empid NUMBER(5),
empname VARCHAR2(30));

INSERT INTO employees (empid, empname) VALUES (1, 'Dan Morgan');
INSERT INTO employees (empid, empname) VALUES (2, 'Jack Cline');
INSERT INTO employees (empid, empname) VALUES (3, 'Caleb Small');
COMMIT;
CREATE OR REPLACE PROCEDURE pass_ref_cur(p_cursor SYS_REFCURSOR) IS

TYPE array_t IS TABLE OF VARCHAR2(4000)
INDEX BY BINARY_INTEGER;

rec_array array_t;

BEGIN
FETCH p_cursor BULK COLLECT INTO rec_array;

FOR i IN rec_array.FIRST .. rec_array.LAST
LOOP
dbms_output.put_line(rec_array(i));
END LOOP;
END pass_ref_cur;
/

set serveroutput on

DECLARE
rec_array SYS_REFCURSOR;
BEGIN
OPEN rec_array FOR
'SELECT empname FROM employees';

pass_ref_cur(rec_array);
CLOSE rec_array;
END;
/



By eygle on 2009-01-09 09:23 | Comments (0) | Posted to Oracle摘 | Edit |

相关文章 随机文章
  • Oracle With 语句语法及示例
  • Merge用法:Oracle 10g中对Merge语句的增强
  • 使用Merge Into 语句实现 Insert/Update
  • Oracle Kernel Layer & ORA-600 Code Info
  • 怎样使用OCI编写多线程的ORACLE应用软件
  • 贸易顺差小词条
    管理学中的经典定律-墨菲定律、马太效应
    孕期孕周与胎儿双顶颈及股骨长参考数据
    实时数据库系统及其特征
    TOM在线拉动中国网络股上涨 网易突破90美元
    网上相关主题:
    Google

    留言 (0)

    发表留言:



    Remember Me?
    (输入验证码后方可评论,谢谢支持)



    CopyRight © 2004 eygle.com, All rights reserved.