`

ref cursor sys_refcursor传递结果集

 
阅读更多
  1. SQL>set linesize 200 pages 100
  2. SQL> variable v refcursor;
  3. SQL>exec open :v for'select * from emp';
  4. PL/SQL procedure successfully completed.
  5. SQL>print:v;
  6. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO DNAME
  7. --------------------------------------------------------------------------------------------------------------------------------
  8. 7369 SMITH CLERK 790217-DEC-8080020 RESEARCH
  9. 7499 ALLEN SALESMAN 769820-FEB-81160030030 noname
  10. 7521 WARD SALESMAN 769822-FEB-81125050030 noname
  11. 7566 JONES MANAGER 783902-APR-81297520 RESEARCH
  12. 7654 MARTIN SALESMAN 769828-SEP-811250140030 noname
  13. 7698 BLAKE MANAGER 783901-MAY-81285030 noname
  14. 7782 CLARK MANAGER 783909-JUN-81245010 ACCOUNTING
  15. 7788 SCOTT ANALYST 756619-APR-87300020 RESEARCH
  16. 7839 KING PRESIDENT 17-NOV-81500010 ACCOUNTING
  17. 7844 TURNER SALESMAN 769808-SEP-811500030 noname
  18. 7876 ADAMS CLERK 778823-MAY-87110020 RESEARCH
  19. 7900 JAMES CLERK 769803-DEC-8195030 noname
  20. 7902 FORD ANALYST 756603-DEC-81300020 RESEARCH
  21. 7934 MILLER CLERK 778223-JAN-82130010 ACCOUNTING
  22. 1111 YODA JEDI 17-NOV-815000 noname
  23. 15 rows selected.
  24. sys_refcursor 做为参数传递结果集:
  25. create or replace procedure pr_sys_refcursor(v_sys out sys_refcursor)
  26. as
  27. BEGIN
  28. open v_sys for'select * from emp';
  29. end;
  30. /
  31. declare
  32. type emp_type is table of emp%rowtype;
  33. emp_tab emp_type;
  34. v sys_refcursor;
  35. BEGIN
  36. pr_sys_refcursor(v);
  37. fetch v bulk collect into emp_tab;
  38. for i in1..emp_tab.count LOOP
  39. dbms_output.put_line(emp_tab(i).ename||','||emp_tab(i).empno);
  40. end loop;
  41. end;
  42. sys_refcursor 函数返回表
  43. create or replace function f_get_emp return sys_refcursor
  44. is
  45. v_emp sys_refcursor;
  46. BEGIN
  47. open v_emp for'select * from emp';
  48. return v_emp;
  49. end;
  50. /
  51. SQL>select f_get_emp from dual;
  52. F_GET_EMP
  53. --------------------
  54. CURSOR STATEMENT :1
  55. CURSOR STATEMENT :1
  56. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO DNAME
  57. --------------------------------------------------------------------------------------------------------------------------------
  58. 7369 SMITH CLERK 790217-DEC-8080020 RESEARCH
  59. 7499 ALLEN SALESMAN 769820-FEB-81160030030 noname
  60. 7521 WARD SALESMAN 769822-FEB-81125050030 noname
  61. 7566 JONES MANAGER 783902-APR-81297520 RESEARCH
  62. 7654 MARTIN SALESMAN 769828-SEP-811250140030 noname
  63. 7698 BLAKE MANAGER 783901-MAY-81285030 noname
  64. 7782 CLARK MANAGER 783909-JUN-81245010 ACCOUNTING
  65. 7788 SCOTT ANALYST 756619-APR-87300020 RESEARCH
  66. 7839 KING PRESIDENT 17-NOV-81500010 ACCOUNTING
  67. 7844 TURNER SALESMAN 769808-SEP-811500030 noname
  68. 7876 ADAMS CLERK 778823-MAY-87110020 RESEARCH
  69. 7900 JAMES CLERK 769803-DEC-8195030 noname
  70. 7902 FORD ANALYST 756603-DEC-81300020 RESEARCH
  71. 7934 MILLER CLERK 778223-JAN-82130010 ACCOUNTING
  72. 1111 YODA JEDI 17-NOV-815000 noname
  73. 15 rows selected.
  74. ref cursor 做为参数传递结果集
  75. create or replace package pkg_ref_cursor
  76. as
  77. type ref_type isref cursor;
  78. function f_ref return ref_type;
  79. end;
  80. /
  81. create or replace package body pkg_ref_cursor
  82. is
  83. function f_ref return ref_type
  84. is
  85. cursor_ref ref_type;
  86. BEGIN
  87. open cursor_ref for'select * from emp';
  88. return cursor_ref;
  89. end;
  90. end;
  91. /
  92. SQL>select pkg_ref_cursor.f_ref from dual;
  93. F_REF
  94. --------------------
  95. CURSOR STATEMENT :1
  96. CURSOR STATEMENT :1
  97. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO DNAME
  98. --------------------------------------------------------------------------------------------------------------------------------
  99. 7369 SMITH CLERK 790217-DEC-8080020 RESEARCH
  100. 7499 ALLEN SALESMAN 769820-FEB-81160030030 noname
  101. 7521 WARD SALESMAN 769822-FEB-81125050030 noname
  102. 7566 JONES MANAGER 783902-APR-81297520 RESEARCH
  103. 7654 MARTIN SALESMAN 769828-SEP-811250140030 noname
  104. 7698 BLAKE MANAGER 783901-MAY-81285030 noname
  105. 7782 CLARK MANAGER 783909-JUN-81245010 ACCOUNTING
  106. 7788 SCOTT ANALYST 756619-APR-87300020 RESEARCH
  107. 7839 KING PRESIDENT 17-NOV-81500010 ACCOUNTING
  108. 7844 TURNER SALESMAN 769808-SEP-811500030 noname
  109. 7876 ADAMS CLERK 778823-MAY-87110020 RESEARCH
  110. 7900 JAMES CLERK 769803-DEC-8195030 noname
  111. 7902 FORD ANALYST 756603-DEC-81300020 RESEARCH
  112. 7934 MILLER CLERK 778223-JAN-82130010 ACCOUNTING
  113. 1111 YODA JEDI 17-NOV-815000 noname
  114. 15 rows selected.
  115. REF 参照类型(ref cursor 程序间传递结果集)
  116. create or replace package ref_package as
  117. TYPE emp_record_type IS RECORD(
  118. ename VARCHAR2(25),
  119. job VARCHAR2(10),
  120. sal NUMBER(7,2));
  121. TYPE weak_ref_cursor IS REF CURSOR;--弱类型,不规定返回值
  122. TYPE strong_ref_cursor IS REF CURSOR return emp%rowtype;--强类型,规定返回值
  123. TYPE strong_ref2_cursor IS REF CURSOR return emp_record_type;--强类型,规定返回值
  124. end ref_package;
  125. /
  126. 弱类型ref测试:
  127. create or replace procedure test_ref_weak(p_deptno emp.deptno%type,
  128. p_cursor out ref_package.weak_ref_cursor)is
  129. begin
  130. case p_deptno
  131. when10then
  132. open p_cursor for
  133. select empno, ename, sal, deptno from emp where deptno = p_deptno;
  134. when20then
  135. open p_cursor for
  136. select*from emp where deptno = p_deptno;
  137. endcase;
  138. end;
  139. /
  140. var c refcursor
  141. exec test_ref_weak(10,:c);--传入不同形式参数,走不同分支,返回不同结果集!
  142. print c
  143. exec test_ref_weak(20,:c);
  144. print c
  145. oracle 9i中定义了系统弱游标类型 sys_refcursor
  146. create or replace procedure test_p(p_deptno number,
  147. p_cursor out sys_refcursor)is
  148. begin
  149. open p_cursor for
  150. select*from emp where deptno = p_deptno;
  151. end test_p;
  152. /
  153. create or replace function getemp return sys_refcursor as
  154. emp_cursor sys_refcursor;
  155. begin
  156. open emp_cursor for
  157. select*from emp;
  158. return emp_cursor;
  159. end;
  160. /
  161. select getemp from dual;
  162. 强类型ref测试:查询结构必须符合游标返回值结构,否则报错:
  163. PLS-00382: expression is of wrong type
  164. create or replace procedure test_ref_strong(p_deptno emp.deptno%type,
  165. p_cursor out ref_package.strong_ref_cursor)is
  166. begin
  167. open p_cursor for
  168. select*from emp where deptno = p_deptno;
  169. end test_ref_strong;
  170. /
  171. var c refcursor
  172. exec test_ref_strong(10,:c);
  173. print c;
  174. create or replace procedure test_call is
  175. c_cursor ref_package.strong_ref_cursor;
  176. r_emp emp%rowtype;
  177. begin
  178. test_ref_strong(10, c_cursor);
  179. loop
  180. fetch c_cursor
  181. into r_emp;
  182. exitwhen c_cursor%notfound;
  183. dbms_output.put_line(r_emp.ename);
  184. end loop;
  185. close c_cursor;
  186. end test_call;
  187. /
  188. exec test_call;
  189. 强类型ref测试:
  190. create or replace procedure test_ref2_strong(p_deptno emp.deptno%type,
  191. p_cursor out ref_package.strong_ref2_cursor)is
  192. begin
  193. open p_cursor for
  194. select ename, job, sal from emp where deptno = p_deptno;
  195. end test_ref2_strong;
  196. /
  197. var c refcursor
  198. exec test_ref2_strong(10,:c);
  1. 11g dbms_sql可以解析出来或者传给外部程序解析
  2. table结合要写管道函数的
  3. DECLARE
  4. TYPE index_by_type IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
  5. index_by_table index_by_type;
  6. v_num number :=1;
  7. BEGIN
  8. --Populate index by table
  9. FOR i IN 7..10 LOOP
  10. index_by_table(v_num):= i;
  11. v_num := v_num +1;
  12. END LOOP;
  13. v_num := index_by_table.first();
  14. dbms_output.put_line(v_num);
  15. v_num := index_by_table.first;
  16. dbms_output.put_line(v_num);
  17. v_num := index_by_table(3);
  18. dbms_output.put_line(v_num);
  19. end;
  20. create or replace package PCKG_CSM_DATA_STL_SET is
  21. TYPE RET_PATH_SET IS TABLE OF NUMBER;
  22. TYPE CV_TYPE IS REF CURSOR;
  23. FUNCTION F_RET_PATH_SET(I_PRTN_ID NUMBER) RETURN RET_PATH_SET PIPELINED;
  24. end;
  25. /
  26. create or replace package body PCKG_CSM_DATA_STL_SET is
  27. FUNCTION F_RET_PATH_SET(I_PRTN_ID NUMBER) RETURN RET_PATH_SET PIPELINED is
  28. CV SYS_REFCURSOR;
  29. V_PRTN_ID PCKG_CSM_DATA_STL_SET.RET_PATH_SET;
  30. RS NUMBER;
  31. BEGIN
  32. OPEN CV FOR select empno from emp where deptno=I_PRTN_ID;
  33. FETCH CV BULK COLLECT
  34. INTO V_PRTN_ID;
  35. RS := V_PRTN_ID.FIRST();
  36. WHILE RS IS NOT NULL LOOP
  37. PIPE ROW(V_PRTN_ID(RS));
  38. RS := V_PRTN_ID.NEXT(RS);
  39. END LOOP;
  40. RETURN;
  41. END F_RET_PATH_SET;
  42. end;
  43. /

 

参考至:http://blog.csdn.net/ashic/article/details/52140232

如有错误,欢迎指正

邮箱:czmcj@163.com

 

 

分享到:
评论

相关推荐

    oracle如何合并多个sys_refcursor详解

    在Oracle数据库环境中,合并多个`sys_refcursor`(也称为游标)的需求常常出现在复杂的业务逻辑中,尤其是在需要重复调用相同逻辑的存储过程时。本文将详细介绍如何通过序列化和XML处理来实现这一功能。 首先,让...

    Oracle游标的使用

    - `sys_refCursor`则代表数据集的引用或指针,它允许数据集的引用在不同的PL/SQL块之间传递,甚至可以传递到其他应用程序(如Java或JDBC)。在示例中,`add_sal`过程返回一个`sys_refCursor`,调用者可以通过`FETCH`...

    plsql.zip_PLSQL Developer_cursor

    v_refcursor SYS_REFCURSOR; BEGIN v_query := 'SELECT * FROM employees WHERE department_id = ' || dept_id; OPEN v_refcursor FOR v_query; RETURN v_refcursor; END; ``` 在这个函数中,我们动态构造了SQL...

    Oracle存储过程返回结果集

    而`SYS_REFCURSOR`是Oracle提供的一种特殊类型,它允许存储过程动态地打开一个游标(即结果集)并将其作为`OUT`参数返回。 以下是一个简单的存储过程示例,它打开一个游标并返回包含`employees`表所有列的结果集: ...

    Oracle Procedurl返回数据集的方式总结

    它允许过程动态地构建SQL查询,并将结果集作为输出参数传递给调用者。这种方式灵活且高效,特别适合处理未知或动态的查询结果。 2. **DECLARE REF CURSOR** 在过程内部,我们可以声明一个REF CURSOR变量,如`REF ...

    ORACLE读取FTP上的EXCEL文件

    RETURN SYS_REFCURSOR IS L_SHEET VARCHAR2(30) := 'Sheet1'; L_QUERY VARCHAR2(200); L_CURSOR SYS_REFCURSOR; BEGIN L_QUERY := 'SELECT * FROM EXTERNAL ''(' || 'SELECT * FROM TABLE(READ_EXCEL_...

    ORACLE 游标

    4. **REF游标**:返回一个引用,可以跨过程或函数调用传递查询结果集。 #### 显式游标示例 显式游标是最常用的类型,下面的例子展示了如何在PL/SQL块中定义和使用显式游标: ```sql DECLARE CURSOR cur_employee...

    Oracle 存储过程分页

    ROWNUM是在结果集生成时自动分配的,表示每一行的顺序号。例如,以下存储过程展示了基于ROWNUM的分页查询: ```sql CREATE OR REPLACE PROCEDURE get_paged_data (start_row IN NUMBER, page_size IN NUMBER, ...

    Oracle数据库通用的分页存储过程

    p_data OUT SYS_REFCURSOR ) AS BEGIN OPEN p_data FOR SELECT * FROM (SELECT A.*, ROWNUM RN FROM (SELECT /*+ FIRST_ROWS(p_page_size) */ * FROM p_table_name WHERE p_where_clause ORDER BY p_column_...

    ORACLE数据库使用动态SQL的方法.pdf

    cursor_name SYS_REFCURSOR; BEGIN stmt := 'SELECT * FROM table WHERE column = :1'; PREPARE sql_statement FROM stmt; EXECUTE sql_statement USING bind_value; OPEN cursor_name FOR stmt USING bind_...

    oracle函数调用存储过程

    CREATE OR REPLACE PROCEDURE plan_station_contrast (p_start_date IN DATE, p_end_date IN DATE, o_cursor OUT SYS_REFCURSOR) AS BEGIN OPEN o_cursor FOR SELECT * FROM some_table WHERE date_column BETWEEN...

    oracle 游标

    cursor_name SYS_REFCURSOR; BEGIN stmt := 'SELECT * FROM table_name WHERE column = :value'; OPEN cursor_name FOR stmt USING 'some_value'; -- 遍历动态游标 FOR record IN cursor_name LOOP -- 处理...

    asp.net中oracle 存储过程(图文)

    CREATE OR REPLACE PROCEDURE gd_CURSOR(MYCS1 OUT SYS_REFCURSOR,MYCS2 OUT SYS_REFCURSOR,a out varchar)as BEGIN a:='test'; OPEN MYCS1 FOR SELECT 1 from dual; OPEN MYCS2 FOR SELECT 2 from dual; END; ...

    Oracle PLSQL应用指南

    Ref Cursor是一种特殊的游标类型,可以作为函数的结果返回,使得调用者可以获取多个结果集。 - **创建Ref Cursor**: ```sql CREATE OR REPLACE FUNCTION get_data (OUT ref_cur SYS_REFCURSOR) AS BEGIN OPEN ...

    oracle 在一个存储过程中调用另一个返回游标的存储过程

    第一种情况是返回的游标是某个具体的表或视图的数据,如: SQL-Code: 代码如下: CREATE OR REPLACE PROCEDURE P_TESTA ( PRESULT OUT SYS_REFCURSOR ) AS BEGIN OPEN PRESULT FOR SELECT * FROM USERS; END P_TESTA;...

    oracle 存储过程分页示例

    ROWNUM是在查询结果集生成时就确定的,表示每一行的顺序号。但需要注意的是,ROWNUM的值是基于查询结果的顺序,而不是表中的物理顺序。如果在WHERE子句中使用ROWNUM,那么Oracle会在满足条件的行中从1开始计数。 ...

    Oracle存储过程返回游标实例详解

    系统游标,通常以`sys_refcursor`类型表示,是Oracle内置的一种方式,用于处理动态SQL的结果集。下面是如何声明和使用系统游标的步骤: - 创建存储过程: ```sql create or replace procedure p_temp_procedure ...

    数据库常用操作

    CREATE OR REPLACE PROCEDURE p_cx_user1 (v_usr_name IN VARCHAR2, aCursor OUT SYS_REFCURSOR) IS BEGIN OPEN aCursor FOR SELECT * FROM sys_user WHERE usr_name = v_usr_name; END; ``` #### 解析: - `...

    Hibernate使用存储过程

    st_cursor SYS_REFCURSOR; BEGIN OPEN st_cursor FOR Select EMPLOYEE, EMPLOYER, STARTDATE, ENDDATE, REGIONCODE, EID, VALUE, CURRENCY FROM EMPLOYMENT; RETURN st_cursor; END; ``` 要在Hibernate中使用...

Global site tag (gtag.js) - Google Analytics