5

尝试使用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用户表类型的用法是个问题。

4

1 回答 1

9

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 ) ;
于 2013-05-17T14:32:00.573 回答