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

后端 未结 5 1311
自闭症患者
自闭症患者 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条回答
  •  萌比男神i
    2020-11-29 16:39

    Another solution is to use an Oracle Collection as a Hashmap:

    declare 
    -- create a type for your "Array" - it can be of any kind, record might be useful
      type hash_map is table of varchar2(1000) index by varchar2(30);
      my_hmap hash_map ;
    -- i will be your iterator: it must be of the index's type
      i varchar2(30);
    begin
      my_hmap('a') := 'apple';
      my_hmap('b') := 'box';
      my_hmap('c') := 'crow';
    -- then how you use it:
    
      dbms_output.put_line (my_hmap('c')) ;
    
    -- or to loop on every element - it's a "collection"
      i := my_hmap.FIRST;
    
      while (i is not null)  loop     
        dbms_output.put_line(my_hmap(i));      
        i := my_hmap.NEXT(i);
      end loop;
    
    end;
    

提交回复
热议问题