尝试使用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用作用户表类型。

最佳答案

您的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 ) ;

09-04 17:34