来自其他对象类型oracle

时间:2018-04-12 16:38:04

标签: oracle plsql collections

我需要将数据附加到已填充的对象类型。要追加的数据取决于已在对象类型中填充的数据。任何人都可以帮我这个。

我尝试了下面的方法,这是正常的。但我想知道是否有更好的方法。

  1. 步骤1:声明并填充对象类型1
  2. 步骤2:声明另一个对象类型2并使用对象类型1填充它
  3. 步骤3:为最终结果集再声明一个对象类型3,并使用对象类型1和对象类型2的组合数据填充
  4. 另外我需要为每个填充的记录分配uniq id(我正在使用 rownum 伪列)

    以下是我尝试的代码,它工作正常。请注意,在实际需求中,我在对象类型中有许多属性,并且在填充对象类型时有许多过滤器和连接。以下是了解解决方案的简化代码。

    -- table EMPLOYEE
    CREATE TABLE EMPLOYEE
    (
         ENAME          VARCHAR2(128)  ,
         EADDRESS       VARCHAR2(200)  ,
         ESTATUS        VARCHAR2(11)   ,
         PARENT_NAME    VARCHAR2(128)   
    );
    
    -- Object
    CREATE OR REPLACE TYPE OBJ_USER_ACTIVITIES AS OBJECT
    (
         UNIQ_ID        NUMBER(9)      ,
         EMPNAME        VARCHAR2(128)  ,
         ADDRESS        VARCHAR2(200)  ,
         STATUS         VARCHAR2(11)   ,
         PARENT_NAME    VARCHAR2(128)            
    );
    
    -- Type
    CREATE OR REPLACE TYPE TAB_USER_ACTIVITIES IS TABLE OF OBJ_USER_ACTIVITIES;
    
    -- Procedure
    CREATE OR REPLACE PROCEDURE Test_Tickets
    IS 
         TEMP_USER_ACTIVITIES_1        TAB_USER_ACTIVITIES := TAB_USER_ACTIVITIES();
         TEMP_USER_ACTIVITIES_2        TAB_USER_ACTIVITIES := TAB_USER_ACTIVITIES();
         TEMP_USER_ACTIVITIES_FINAL    TAB_USER_ACTIVITIES := TAB_USER_ACTIVITIES();
    
         i integer;
    
    BEGIN
    
         -- First resultset
         SELECT OBJ_USER_ACTIVITIES
         (ROWNUM, EMPNAME, ADDRESS, STATUS, PARENT_NAME)
         BULK COLLECT INTO TEMP_USER_ACTIVITIES_1
         FROM
         (
              SELECT    ENAME AS EMPNAME, EADDRESS AS ADDRESS, ESTATUS AS STATUS, PARENT_NAME
              FROM      EMPLOYEE
              --WHERE   SOME FILTER
         );
    
         i := TEMP_USER_ACTIVITIES_1.COUNT;
    
         -- Build second resultset depending on valus of first resultset
         SELECT OBJ_USER_ACTIVITIES
         ( i + ROWNUM, EMPNAME, ADDRESS, STATUS, PARENT_NAME )
         BULK COLLECT INTO TEMP_USER_ACTIVITIES_2
         FROM
         (
              SELECT    ENAME AS EMPNAME, EADDRESS AS ADDRESS, ESTATUS AS STATUS, PARENT_NAME
              FROM      EMPLOYEE
              WHERE     ENAME IN ( SELECT PARENT_NAME FROM TABLE(TEMP_USER_ACTIVITIES_1) WHERE PARENT_NAME IS NOT NULL )           
         );
    
    
         -- FINAL resultset combining above two resulset
         SELECT OBJ_USER_ACTIVITIES
         ( UNIQ_ID, EMPNAME, ADDRESS, STATUS, PARENT_NAME )
         BULK COLLECT INTO TEMP_USER_ACTIVITIES_FINAL
         FROM
         (
              SELECT    UNIQ_ID, EMPNAME, ADDRESS, STATUS, PARENT_NAME
              FROM      TABLE(TEMP_USER_ACTIVITIES_1)
              UNION
              SELECT    UNIQ_ID, EMPNAME, ADDRESS, STATUS, PARENT_NAME
              FROM      TABLE(TEMP_USER_ACTIVITIES_2)
         );
    
    END;
    

1 个答案:

答案 0 :(得分:1)

为此,您可以使用一组很好的嵌套表运算符。 HEre就是一个例子(其他一些运营商只是为了完整而注释掉了)

SQL> create or replace type MY_OBJ as object ( x int, y int );
  2  /

Type created.

SQL>
SQL> create or replace type MY_OBJ_LIST as table of my_obj
  2  /

Type created.

SQL>
SQL> set serverout on
SQL> declare
  2    t1 my_obj_list :=
  3      my_obj_list(
  4        my_obj(1,2),
  5        my_obj(3,4),
  6        my_obj(5,6));
  7
  8    t2 my_obj_list :=
  9      my_obj_list(
 10        my_obj(7,8),
 11        my_obj(9,10),
 12        my_obj(11,12));
 13
 14    t3  my_obj_list := my_obj_list();
 15  begin
 16   t3 :=  t1 MULTISET UNION t2;
 17
 18   dbms_output.put_line('I now have '||t3.count||' elements');
 19
 20  --  t3 := t1 multiset union t2;
 21  --  t3 := t1 multiset union distinct t2;
 22  --  t3 := t1 multiset intersect t2;
 23  --  t3 := t1 multiset intersect distinct t2;
 24  --  t3 := set(t2);
 25  --  t3 := t1 multiset except t2;
 26  --  t3 := t1 multiset except distinct t2;
 27
 28  end;
 29  /
I now have 6 elements

PL/SQL procedure successfully completed.