在很多情况下,我们需要通过bind var來提高整个DB的performance,那么,我们应当如何在procedure中完成对结果集的查询呢?从Oracle7.3才被支持,在Oracle 9i以后又有新的变化,在Oracle 9i以前要define一个type才可以。而在Oracle 9i以后Oracle引入了一 个新的类型sys_refcursor,这样我们就不用重新定义了。以下是一个具体的示例:
C:oracleora92sqlplusdemo>sqlplus /nolog
SQL*Plus: Release 9.2.0.1.0 - Production on 星期六 4月 2 11:09:06 2005
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
SQL> conn scott/tiger@vongates
connected.
SQL> create or replace procedure getEmpByDept(in_deptNo in emp.deptno%type,
2 out_curEmp out SYS_REFCURSOR) as
3
4 begin
5 open out_curEmp for
6 SELECT * FROM emp WHERE deptno = in_deptNo ;
7 EXCEPTION
8 WHEN OTHERS THEN
9 RAISE_APPLICATION_ERROR(-20101,
10 'Error in getEmpByDept' || SQLCODE );
12 end getEmpByDept;
13 /
已建立程序.
SQL> var rset refcursor;
SQL> exec getEmpByDept(10,:rset);
PL/SQL 程序順利完成.
SQL> print rset;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7782 CLARK MANAGER 7839 09-1月 -81 2450 10
7839 KING PRESIDENT 17-11月-81 5000 10
SQL>
/ |
另一个例子:
CREATE OR REPLACE PROCEDURE P_RETR_CURSOR
(P_ID IN NUMBER,
P_RECSET OUT SYS_REFCURSOR) AS
--Created by xsb on 2004-11-09 For: 存储过程返回记录集
BEGIN
OPEN P_RECSET FOR
SELECT *
FROM t
WHERE c1 = P_ID;
END;
---------------------------------------
DECLARE
--Created by xsb on 2004-11-09 For: 存储过程返回记录集测试
--set serveroutput on
I NUMBER;
S VARCHAR2(100);
M SYS_REFCURSOR;
BEGIN
FOR J IN 1 .. 10 LOOP
P_RETR_CURSOR(J, M);
FETCH M
INTO S, I;
DBMS_OUTPUT.PUT(I || '-');
DBMS_OUTPUT.PUT_LINE(S);
CLOSE M;
END LOOP;
END; | |
阅读(287) | 评论(0) | 转发(0) |