如下package:
CREATE OR REPLACE package SCOTT.pk_wt
is
type mytype is ref cursor;
procedure p_wt(mycs out mytype);
end;
CREATE OR REPLACE package BODY SCOTT.pk_wt
is
procedure p_wt(mycs out mytype)
is
r_cur mytype;
begin
open r_cur for select * from emp;
end p_wt;
end pk_wt;
这个package是我的package的简化版,有这样一个package,我想在查询分析器中或者sqlplus能直接调用这个package,其实就是想利用package来查询一张表的结果,求各位大神指点下
------解决思路----------------------
写个例子给你,利用table()函数,使用select * from来输出游标中的结果集
不过如果结果集较大,不建议这么用
SQL> CREATE OR REPLACE package pk_wt
2 is
3 type mytype is ref cursor;
4 procedure p_wt(mycs out mytype);
5 end;
6 /
Package created
SQL>
SQL> CREATE OR REPLACE package BODY pk_wt
2 is
3 procedure p_wt(mycs out mytype)
4 is
5 begin
6 open mycs for select * from scott.emp;
7 end p_wt;
8 end pk_wt;
9 /
Package body created
SQL>
SQL> create type tp_point1 as object(empno NUMBER(4),ENAME VARCHAR2(10),JOB VARCHAR2(9),MGR NUMBER(4),HIREDATE DATE
2 ,SAL NUMBER(7,2),COMM NUMBER(7,2),DEPTNO NUMBER(2))
3 /
Type created
SQL>
SQL> create type tp_point1newt as table of tp_point1
2 /
Type created
SQL>
SQL> create or replace function func_wt return tp_point1newt pipelined
2 as
3 v_mycs pk_wt.mytype;
4 v_empno NUMBER(4);
5 v_ENAME VARCHAR2(10);
6 v_JOB VARCHAR2(9);
7 v_MGR NUMBER(4);
8 v_HIREDATE DATE;
9 v_SAL NUMBER(7,2);
10 v_COMM NUMBER(7,2);
11 v_DEPTNO NUMBER(2);
12 begin
13 pk_wt.p_wt(v_mycs);
14 loop
15 fetch v_mycs into v_empno,v_ENAME,v_JOB,v_MGR,v_HIREDATE,v_SAL,v_COMM,v_DEPTNO;
16 exit when v_mycs%notfound;
17 pipe row(tp_point1(v_empno,v_ENAME,v_JOB,v_MGR,v_HIREDATE,v_SAL,v_COMM,v_DEPTNO));
18 end loop;
19 return;
20 end;
21 /
Function created
SQL> select * from table(func_wt);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- ----------- ---------- ---------- ----------
7369 SMITH CLERK 7902 1980/12/17 800 20
7499 ALLEN SALESMAN 7698 1981/02/20 1600 300 30
7521 WARD SALESMAN 7698 1981/02/22 1250 500 30
7566 JONES MANAGER 7839 1981/04/02 2975 20
7654 MARTIN SALESMAN 7698 1981/09/28 1250 1400 30
7698 BLAKE MANAGER 7839 1981/05/01 2850 30
7782 CLARK MANAGER 7839 1981/06/09 2450 10
7788 SCOTT ANALYST 7566 1987/04/19 3000 20
7839 KING PRESIDENT 1981/11/17 5000 10
7844 TURNER SALESMAN 7698 1981/09/08 1500 0 30
7876 ADAMS CLERK 7788 1987/05/23 1100 20
7900 JAMES CLERK 7698 1981/12/03 950 30
7902 FORD ANALYST 7566 1981/12/03 3000 20
7934 MILLER CLERK 7782 1982/01/23 1300 10
14 rows selected
SQL>