如何使用集合在PL / SQL中实现物化视图

时间:2015-08-18 12:33:41

标签: sql plsql oracle11g

我是PL / SQL的新手。我创建了一个简单的物化视图,我必须使用集合转换PL / SQL。

任何人都可以给我逻辑如何实现这个目标吗?

CREATE MATERIALIZED VIEW EMP_VW(ENAME,DNAME,JOB,SAL,COMM,DEPTNO)
AS 
    SELECT              IV.EMP_NAME,
                        IV.JOB,
                        IV.SALARY,
                        IV.COMMISSION,
                        IV.DEPARTMENT_NO,
                        IV.DEPARTMENT_NAME
     FROM(  SELECT                  EMP.ENAME       AS  EMP_NAME,
                                    EMP.JOB         AS  JOB,
                                    EMP.SAL         AS  SALARY,
                                    EMP.COMM        AS  COMMISSION,
                                    EMP.DEPTNO      AS  DEPARTMENT_NO,
                                    DEPT.DNAME      AS  DEPARTMENT_NAME
           FROM         SCOTT.EMP INNER JOIN SCOTT.DEPT
           ON           EMP.DEPTNO=DEPT.DEPTNO
           AND          EMP.DEPTNO IN(10,20)
           ORDER        BY  EMP.DEPTNO) IV;

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的请求,那么您就可以将select语句的返回值拉入集合中。

DECLARE
   CURSOR my_cur IS
    SELECT              IV.EMP_NAME,
                        IV.JOB,
                        IV.SALARY,
                        IV.COMMISSION,
                        IV.DEPARTMENT_NO,
                        IV.DEPARTMENT_NAME
     FROM(  SELECT                  EMP.ENAME       AS  EMP_NAME,
                                    EMP.JOB         AS  JOB,
                                    EMP.SAL         AS  SALARY,
                                    EMP.COMM        AS  COMMISSION,
                                    EMP.DEPTNO      AS  DEPARTMENT_NO,
                                    DEPT.DNAME      AS  DEPARTMENT_NAME
           FROM         SCOTT.EMP INNER JOIN SCOTT.DEPT
           ON           EMP.DEPTNO=DEPT.DEPTNO
           AND          EMP.DEPTNO IN(10,20)
           ORDER        BY  EMP.DEPTNO) IV;

   TYPE my_tab_type IS TABLE OF my_cur%ROWTYPE;

   my_tab MY_TAB_TYPE;
BEGIN
   OPEN my_cur;
   FETCH my_cur BULK COLLECT INTO my_tab;
   CLOSE my_cur;
END;
/