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 )。
答案 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;
我个人会审核所有的命名,因为dname
,nt_dname_dname
和t_d
至少对我来说有点混乱。