【问题标题】:BULK COLLECT into a table of objectsBULK COLLECT 到对象表中
【发布时间】:2013-05-12 17:19:06
【问题描述】:

尝试使用BULK COLLECT 语句时,我收到错误ORA-00947: not enough values

一个示例脚本:

CREATE OR REPLACE 
TYPE company_t AS OBJECT ( 
   Company          VARCHAR2(30),
   ClientCnt            INTEGER   );
/

CREATE OR REPLACE 
TYPE company_set AS TABLE OF company_t;    
/

CREATE OR REPLACE 
FUNCTION piped_set (
  v_DateBegin IN DATE,
  v_DateEnd IN DATE
)
return NUMBER /*company_set pipelined*/ as
  v_buf company_t := company_t( NULL, NULL);
  atReport company_set;
  sql_stmt VARCHAR2(500) := '';
begin

select * BULK COLLECT INTO atReport
from (
   SELECT 'Descr1', 1 from dual
   UNION
   SELECT 'Descr2', 2 from dual ) ;

  return 1;
end;

错误发生在select * BULK COLLECT INTO atReport这一行。

顺便说一下,直接 PL/SQL 可以正常工作(因此无需将其作为解决方案提及)。在用户表类型中使用BULK COLLECT 是个问题。

【问题讨论】:

    标签: oracle plsql oracle11g bulkinsert usertype


    【解决方案1】:

    您的company_set 是一个对象 表,您选择的是值,而不是由这些值组成的对象。这将编译:

    select * BULK COLLECT INTO atReport
    from (
       SELECT company_t('Descr1', 1) from dual
       UNION
       SELECT company_t('Descr2', 2) from dual ) ;
    

    ...但是运行时会抛出ORA-22950: cannot ORDER objects without MAP or ORDER method,因为union 执行隐式排序来识别和删除重复项,因此请改用union all

    select * BULK COLLECT INTO atReport
    from (
       SELECT company_t('Descr1', 1) from dual
       UNION ALL
       SELECT company_t('Descr2', 2) from dual ) ;
    

    【讨论】:

    • 谢谢@Alex,代码真的很有帮助!作为问题的延续,是否可以将批量插入的结果作为流水线函数结果传递?目前,我将结果输入 atReport ant 然后执行“FOR .. LOOP ( pipe_row (atReportRow) ) END LOOP; ”。也许这部分也可以简化?
    • @xacinay - 不适用于bulk collect;您可以将其作为光标循环来代替(for rec in (select company_t() as comp ...) loop pipe row rec.comp; end loop; 或类似的。我想性能会相似。如果您尝试这样做并且无法使其工作,您可能应该问一个新问题。
    • 这正是它的工作方式:for x in (select ..) pipe row ( company_t( x.Company, x.ClientCnt) ) )。我只是想知道,它可以更简单。无论如何,目前的结果很好,谢谢!
    猜你喜欢
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多