PL / SQL:ORA-00947:值不足

时间:2019-09-25 04:49:07

标签: oracle procedure nested-table object-type bulk-collect

我正在创建一个程序来显示n个雇员的最高和最低工资。如果我将输入5,则查询将为我获得5个员工的最高和最低薪水。

对于上述情况,我创建了一个具有以下两列的对象

create type vrec as object(
empno number,
sal number
);
/

然后我在对象类型的帮助下创建了嵌套表,这样我就可以将嵌套表用作out参数,一秒钟就返回所有行。

create type vrec_type is table of vrec;
/

创建数据类型后,我将创建如下所示的过程

create or replace procedure pro_first_last(input in number,salary out vrec_type)
is
begin
select empno,sal BULK COLLECT INTO salary from (
select empno,sal from  
(select empno,sal,rank() over(order by sal asc) min_sal from emp5 where sal is not null) where min_sal <= 5
union all
select empno,sal from 
(select empno,sal,rank() over(order by sal desc) max_sal from emp5 where sal is not null) where max_sal <= 5);
for i in salary.first..salary.last
loop
dbms_output.put_line(salary(i).empno);
end loop;
end;
/

当我编译以上过程时,即时通讯没有足够的值。我还创建了具有两列的对象,并且在select语句中我也仅返回了两列。有人可以对此进行审查并为我提供帮助,还是提供其他解决方案。

1 个答案:

答案 0 :(得分:1)

您直接将empno, sal值添加到salaryvrec_type对象中,该值只能采用对象类型vrec的值)

您需要创建object中的vrec,然后将其添加到salary中,如下所示:

create or replace procedure pro_first_last(input in number,salary out vrec_type)
is
begin
  select vrec(empno,sal) -- change in this line
         BULK COLLECT INTO salary from ( 
    select empno,sal from  
      (select empno,sal,rank() over(order by sal asc) min_sal from emp5 where sal is not null) where min_sal <= 5
    union all
    select empno,sal from 
     (select empno,sal,rank() over(order by sal desc) max_sal from emp5 where sal is not null) where max_sal <= 5);
    for i in salary.first..salary.last
      loop
        dbms_output.put_line(salary(i).empno);
    end loop;
end;

干杯!