如何从pl sql表

时间:2016-10-21 15:31:44

标签: plsql oracle11g nested-table

我创建了嵌套表,如下所示:

CREATE OR REPLACE TYPE  EMP_NO_NAME 
AS OBJECT 
( 
EMPNO NUMBER(4),
ENAME VARCHAR2(20),
JOB VARCHAR2(20),
MGR NUMBER(5),
HIREDATE DATE,
SAL NUMBER(7,2)
);
CREATE OR REPLACE TYPE EMP_TABLE IS TABLE OF EMP_NO_NAME;

-----------------------
CREATE TABLE NESTED_EMP 
(
DEPTNO NUMBER(2) ,
EMPLOYEE EMP_TABLE
)
NESTED TABLE EMPLOYEE STORE AS NESTED_EMPLOYEE;

INSERT INTO NESTED_EMP (DEPTNO,EMPLOYEE)
VALUES (10,EMP_TABLE(EMP_NO_NAME(7839,'KING','PRESIDENT',NULL,'17-NOV-81',5000),
                     EMP_NO_NAME(7782,'CLARK','MANAGER',7839,'09-JUN-81',2450),
                     EMP_NO_NAME(7934,'MILLER','CLERK',7782,'23-JAN-82',1300)
                     )
        );   

INSERT INTO NESTED_EMP (DEPTNO,EMPLOYEE)
VALUES (20,EMP_TABLE(EMP_NO_NAME(7566,'JONES','MANAGER',7839,'02-APR-81',2975),
                      EMP_NO_NAME(7902,'FORD','ANALYST',7566,'03-DEC-81',3000),
                      EMP_NO_NAME(7369,'SMITH','CLERK',7902,'17-DEC-80',800),
                      EMP_NO_NAME(7788,'SCOTT','ANALYST',7566,'09-DEC-82',3000),
                      EMP_NO_NAME(7876,'ADAMS','CLERK',7788,'12-JAN-83',1100)
                      )
       ); 
INSERT INTO NESTED_EMP (DEPTNO,EMPLOYEE)
VALUES (20,EMP_TABLE(EMP_NO_NAME(7698,'BLAKE','MANAGER',7839,'01-MAY-81',2850),
                     EMP_NO_NAME(7654,'MARTIN','SALESMAN',7698,'28-SEP-81',1250),
                     EMP_NO_NAME(7499,'ALLEN','SALESMAN',7698,'20-FEB-81',1600),
                     EMP_NO_NAME(7844,'TURNER','SALESMAN',7698,'08-SEP-81',1500),
                     EMP_NO_NAME(7900,'JAMES','CLERK',7698,'03-DEC-81',950),
                     EMP_NO_NAME(7521,'WARD','SALESMAN',7698,'22-FEB-81',1250)
                     )
      );

现在我在plsql中获取嵌套表的值:

    DECLARE 
    CURSOR EMPLOYEE IS 
    select p.* from NESTED_EMP p1 ,table(p1.employee) p;
    V_EMP EMP_TABLE;
    BEGIN 
    FOR  V_EMP IN EMPLOYEE
    LOOP
    EXIT WHEN EMPLOYEE%NOTFOUND;
    END LOOP;
    FOR MYINDEX IN V_EMP.FIRST..V_EMP.LAST
    LOOP 
    DBMS_OUTPUT.PUT_LINE(V_EMP(MYINDEX).ENAME);
    END LOOP;
    END;
    / 
END;

错误报告:

  

ORA-06531:未初始化集合ORA-06512的参考:第10行   06531. 00000 - "参考未初始化的集合"

     

*原因:嵌套表或varray的元素或成员函数              被引用(需要初始化集合)              没有收集已经初始化。

     

*操作:使用适当的构造函数初始化集合              或整个对象分配。

如何在plsql表中获取嵌套表值?

1 个答案:

答案 0 :(得分:2)

您的代码存在的问题是V_EMP实际上不是EMP_TABLE类型。相反,它是EMPLOYEE.ROWTYPE。初始化游标for循环时,变量会自动变为适当的ROWTYPE,覆盖之前的任何声明。

好消息是,由于您已经在查询中引用了嵌套表,因此您不需要在循环中执行此操作(它已经被爆炸)。您的PL / SQL可以大大简化:

DECLARE
   CURSOR employee IS
      SELECT p.*
      FROM   nested_emp p1 CROSS JOIN TABLE (p1.employee) p;
BEGIN
   FOR v_emp IN employee LOOP
      DBMS_OUTPUT.put_line (v_emp.ename);
   END LOOP;
END;
/

您会注意到EXIT WHEN也被删除了。循环游标在最后一条记录后自动终止。

另一种方法是不在查询中分解嵌套表。那你需要两个循环:

DECLARE
   CURSOR employee IS
      SELECT p.*
      FROM   nested_emp p;
BEGIN
   FOR v_emp IN employee LOOP
      for i in v_emp.employee.first..v_emp.employee.last loop
        DBMS_OUTPUT.put_line (v_emp.employee(i).ename);
      end loop;
   END LOOP;
END;
/