Oracle存储有临时表的函数

时间:2015-04-27 12:05:29

标签: oracle function plsql temporary

我创建了一个临时表和一个存储函数来读取它。当我打电话给它时会出现以下消息:

RA-22905: Zugriff auf Zeilen eines Objekts, das keine Nested Table ist, nicht möglich
22905. 00000 -  "cannot access rows from a non-nested table item"
*Cause:    attempt to access rows of an item whose type is not known at
           parse time or that is not of a nested table type
*Action:   use CAST to cast the item to a nested table type
Fehler in Zeile: 3 Spalte: 15

那么,我怎么能做CAST的事呢?

我心爱的功能:

create or replace PACKAGE BODY testlho2 IS 
  FUNCTION getBasicDate (app_in IN varchar2, termc_in IN varchar2) 
    return sys_refcursor is 
    l_rc SYS_REFCURSOR; 
  BEGIN 
    -- Populate temporary table 
    INSERT INTO temp_tab_test_lho2 (app, sla, tsl) 
      SELECT app, sla, tslstat 
      FROM pmon_orig_file 
      WHERE app = app_in and termcause = termc_in; 
    -- Open REF CURSOR for Output 
    open l_rc for 
      select app, sla, tsl 
      from temp_tab_test_lho2; 
    return l_rc; 
  END; 
END testlho2 ;

1 个答案:

答案 0 :(得分:0)

临时表:

create global temporary table tbl(name_ varchar2(50))
/

功能:

create or replace function foo(app_in IN varchar2)
return sys_refcursor is 
l_rc SYS_REFCURSOR; 
begin
  insert into tbl select app_in from dual;
  open l_rc for select name_ from tbl;
  return l_rc;
end;
/

从功能中选择数据:

select * from table(foo('hiiii'));

错误:

ORA-22905: cannot access rows from a non-nested table item
22905. 00000 -  "cannot access rows from a non-nested table item"

如果想要使用上述功能,则必须返回type。 见下面的问题:

Function return sys_refcursor call from sql with specific columns

如果您在匿名块中使用该功能,那么它将起作用:

declare
l_rc SYS_REFCURSOR; 
begin
l_rc := foo('hiii');
end;
/

anonymous block completed

即使您使用type解决了返回类型问题,如果您在select语句中使用该函数,那么它将错误地说:

ORA-14551: cannot perform a DML operation inside a query 
14551. 00000 -  "cannot perform a DML operation inside a query "

这里说明了如何解决上述问题

Solution to "cannot perform a DML operation inside a query"?