2011-01-06 52 views
3

我做以下,但它不工作如何调用一个函数在一个包

select package_name.function_name(param,param) from dual 

我打电话,返回一个指针,以便即时猜测"from dual"一个功能问题

是还有另一种做法吗?

+1

什么是“不工作”的意思是 - 你得到了什么错误?你的'游标'在包规范中是如何定义的?如果它是一个`sys_refcursor`并且该函数没有任何令人不愉快的副作用,那么从dual选择应该可以工作... – 2011-01-06 12:30:38

+0

我想这个问题与您之前的问题有关:http://stackoverflow.com/questions/4613973/ returned-ref-cursor-not-supported不要将问题发布到同一主题上。如果您没有得到满意的答案,请立即编辑您的问题以提供更多详细信息。 – APC 2011-01-06 13:41:00

回答

2

我认为你的意思是一个参考游标。这是一个PL/SQL结构,它充当指向由查询返回的一组记录的指针。这意味着它必须由运行查询的客户端来解释。例如,我们可以将Ref Cursor映射到JDBC或ODBC ResultSet。

你的基本陈述肯定没有错。这是类似于你自己的一个函数:

SQL> desc get_emps 
FUNCTION get_emps RETURNS REF CURSOR 
Argument Name     Type     In/Out Default? 
------------------------------ ----------------------- ------ -------- 
P_DNO       NUMBER(2)    IN 
P_SORT_COL      VARCHAR2    IN  DEFAULT 
P_ASC_DESC      VARCHAR2    IN  DEFAULT 

SQL> 

我可以很容易地把这个放在更广阔的PL/SQL块:

SQL> declare 
    2  rc sys_refcursor; 
    3 begin 
    4  rc := get_emps(50); 
    5 end; 
    6/

PL/SQL procedure successfully completed. 

SQL> 

但是,SQL * PLUS可以处理CURSOR构造本身:

SQL> select get_emps(50) from dual 
    2/

GET_EMPS(50) 
-------------------- 
CURSOR STATEMENT : 1 

CURSOR STATEMENT : 1 

    EMPNO ENAME  JOB    MGR HIREDATE   SAL  COMM  DEPTNO 
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 
     8060 VERREYNNE PLUMBER   8061 08-APR-08  4000     50 
     8061 FEUERSTEIN PLUMBER   7839 27-FEB-10  4500     50 
     8085 TRICHLER PLUMBER   8061 08-APR-10  3500     50 
     8100 PODER  PLUMBER   8061     3750     50 


SQL> 

这条语句也在SQL Developer中运行,尽管结果集以丑陋的方式进行了布局。

所以,如果您有您的功能问题,问题是:

  1. 什么客户端环境您使用的?
  2. 它以什么确切的方式“不起作用”?请描述观察到的行为,包括任何错误消息?
  3. 也给我们环境的详细信息,如版本的数据库,操作系统等

看了你的其他问题,关于这个话题我想这个问题可能是由于使用的用户定义参考游标(而不是内置)。但是,这没有任何区别。这个打包的功能:

SQL> create or replace package emp_rc_utils as 
    2 
    3  type emp_rc is ref cursor return emp%rowtype; 
    4 
    5  function  get_emps 
    6   (p_dno in emp.deptno%type 
    7  ) 
    8  return emp_rc; 
    9 end; 
10/

Package created. 

SQL> create or replace package body emp_rc_utils as 
    2 
    3  function  get_emps 
    4   (p_dno in emp.deptno%type 
    5  ) 
    6   return emp_rc 
    7  is 
    8   return_value emp_rc_utils.emp_rc; 
    9  begin 
10 
11   open return_value for select * from emp where deptno = p_dno; 
12 
13   return return_value; 
14  end get_emps; 
15 
16 end emp_rc_utils; 
17/

Package body created. 

SQL> 

仍然工程...

SQL> declare 
    2  rc sys_refcursor; 
    3 begin 
    4  rc := emp_rc_utils.get_emps(50); 
    5 end; 
    6/

PL/SQL procedure successfully completed. 


SQL> select emp_rc_utils.get_emps(50) from dual 
    2/

EMP_RC_UTILS.GET_EMP 
-------------------- 
CURSOR STATEMENT : 1 

CURSOR STATEMENT : 1 

    EMPNO ENAME  JOB    MGR HIREDATE   SAL  COMM  DEPTNO 
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 
     8085 TRICHLER PLUMBER   8061 08-APR-10  3500     50 
     8060 VERREYNNE PLUMBER   8061 08-APR-08  4000     50 
     8061 FEUERSTEIN PLUMBER   7839 27-FEB-10  4500     50 
     8100 PODER  PLUMBER   8061     3750     50 


SQL> 
0

你尝试过:

myCursor := package_name.function_name(param,param); 

这必须是从测试块或存储过程中。

+0

我不知道你的意思 – code511788465541441 2011-01-06 12:09:34

1

您可以通过一个指针来完成这一操作或填充用户定义的表,并将其返回如下:

create or replace 
function getRef return sys_refcursor 
is 
l_ref sys_refcursor; 
begin 

    open l_ref for 
    select 1 a, 'a' c from dual 
    union all 
    select 2 a, 'b' c from dual 
    union all 
    select 3 a, 'c' c from dual 
    union all 
    select 4 a, 'd' c from dual; 

    return l_ref; 

end getRef; 
/

select getref() from dual; 

GETREF() 
-------- 
A      C 
---------------------- - 
1      a 
2      b 
3      c 
4      d 

--you'll notice this isn't the most user-friendly result set if you look at it in SQL Developer or whatno 
--drop function getRef; 

,你也可以使用“表”如果你传回的表收集这样

create or replace type lookup_row as 
    object (a number, c varchar2(20)); 
/
create or replace type lookups_tab as 
    table of lookup_row; 
/

create or replace 
function getUserDefinedTableType return lookups_tab 
is 
lTestTypeTable lookups_tab; 
begin 

    SELECT lookup_row(a,c) 
       bulk collect INTO lTestTypeTable 
       from 
    (select 1 a, 'a' c from dual 
    union all 
    select 2 a, 'b' c from dual 
    union all 
    select 3 a, 'c' c from dual 
    union all 
    select 4 a, 'd' c from dual); 

    return lTestTypeTable; 

end getUserDefinedTableType; 
/


select * from table(getUserDefinedTableType()); 
--this returns it in a more user friendly manner 
--http://www.oreillynet.com/pub/a/network/2003/01/22/feuerstein.html?page=2 
--http://stackoverflow.com/questions/3150137/converting-oracle-query-into-user-defined-types-in-pl-sql/3152885#3152885 
A      C      
---------------------- -------------------- 
1      a      
2      b      
3      c      
4      d 
+0

你能否请你多解释一下这两个解决方案是什么以及他们在做什么。 – code511788465541441 2011-01-06 14:49:22

相关问题