Oracle PL/SQL - How to create a simple array variable?

后端 未结 5 1316
自闭症患者
自闭症患者 2020-11-29 16:06

I\'d like to create an in-memory array variable that can be used in my PL/SQL code. I can\'t find any collections in Oracle PL/SQL that uses pure memory, they all seem to b

5条回答
  •  鱼传尺愫
    2020-11-29 16:45

    You could just declare a DBMS_SQL.VARCHAR2_TABLE to hold an in-memory variable length array indexed by a BINARY_INTEGER:

    DECLARE
       name_array dbms_sql.varchar2_table;
    BEGIN
       name_array(1) := 'Tim';
       name_array(2) := 'Daisy';
       name_array(3) := 'Mike';
       name_array(4) := 'Marsha';
       --
       FOR i IN name_array.FIRST .. name_array.LAST
       LOOP
          -- Do something
       END LOOP;
    END;
    

    You could use an associative array (used to be called PL/SQL tables) as they are an in-memory array.

    DECLARE
       TYPE employee_arraytype IS TABLE OF employee%ROWTYPE
            INDEX BY PLS_INTEGER;
       employee_array employee_arraytype;
    BEGIN
       SELECT *
         BULK COLLECT INTO employee_array
         FROM employee
        WHERE department = 10;
       --
       FOR i IN employee_array.FIRST .. employee_array.LAST
       LOOP
          -- Do something
       END LOOP;
    END;
    

    The associative array can hold any make up of record types.

    Hope it helps, Ollie.

提交回复
热议问题