具有返回类型嵌套表的Oracle-PL / SQL函数错误类型不完整或格式错误

时间:2017-05-21 08:52:15

标签: oracle plsql nested-table

CREATE TYPE dname AS OBJECT
(
  depno      NUMBER,
  cnt_dname  NUMBER
);
/

CREATE TYPE nt_dname_dname AS TABLE of dname;
/

CREATE OR REPLACE FUNCTION f1 RETURN nt_dname 
AS    
    nt_dname        t_d     := t_d();
    n               NUMBER  := 0;
BEGIN
    FOR x IN 
    (
        SELECT  department_id, count(employee_id)
        FROM    employees
        GROUP BY department_id
    )
    LOOP
        nt_dname.EXTEND;
        n               := n+1;
        nt_dname(n)     := dname(x.depno,x.cnt_dname);
    END LOOP; 

    RETURN t_d;
END;
/   

我收到以下错误消息

Error: PL/SQL: Compilation unit analysis terminated
Error(1,20): **PLS-00320: the declaration of the type of this expression is 
incomplete or malformed**

请建议哪里出错了。我想要得到 每个部门的员工数量并将其作为输出( RETURN nested_table )。

1 个答案:

答案 0 :(得分:0)

以下编译,但我还没有对其进行测试:

create or replace function f1
    return nt_dname_dname
as
    t_d nt_dname_dname := nt_dname_dname();
    n   number := 0;
begin
    for r in (
        select department_id as depno
             , count(employee_id) as cnt_dname
        from   employees
        group  by department_id
    )
    loop
        t_d.extend;
        n := n + 1;
        t_d(n) := dname(r.depno, r.cnt_dname);
    end loop;

    return t_d;
end;

我个人会审核所有的命名,因为dnament_dname_dnamet_d至少对我来说有点混乱。