【问题标题】:Oracle stored function with temp table带有临时表的 Oracle 存储函数
【发布时间】:2015-07-05 21:05:41
【问题描述】:

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

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 ;

【问题讨论】:

  • 对不起,我的水晶球坏了。如果你发布你的函数,也许有人会说如何修复它。
  • 创建或替换PACKAGE BODY testlho2 IS FUNCTION getBasicDate (app_in IN varchar2, termc_in IN varchar2) return sys_refcursor is l_rc SYS_REFCURSOR; BEGIN (-- 填充临时表) 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;返回 l_rc;结尾; END testlho2 ;
  • 这是我心爱的功能
  • 乍一看,我没有看到 ORA-22905 的任何原因。表字段都是按顺序输入的吗?顺便说一句,你真的需要临时表吗?为什么不直接从 pmon_orig_file 返回选择?

标签: oracle function plsql temporary


【解决方案1】:

临时表:

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"?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    相关资源
    最近更新 更多