Oracle

檢索關聯數組的索引作為集合

  • October 28, 2012

在 PL/SQL 中,假設我有一些關聯數組定義如下:

declare 
      type a_arr_t is table of PLS_INTEGER index by PLS_INTEGER;
      a_arr a_arr_t;

然後,我稀疏地填充數組,如下所示:

begin
     a_arr(1)   := 2;
     a_arr(10)  := 4;
     a_arr(100) := 6;
end;

是否有一些運算符或函式可以給我數組的索引,(1,10,100)作為某種集合,就像indices offorall語句中一樣?

是否有一些運算符或函式可以給我數組的索引

不,您必須遍歷關聯數組:

declare 
      type a_arr_t is table of PLS_INTEGER index by PLS_INTEGER;
      type keys_t is table of PLS_INTEGER;
      a_arr a_arr_t;
      keys_ keys_t := keys_t();
      l_index integer;
begin
     a_arr(1)   := 2;
     a_arr(10)  := 4;
     a_arr(100) := 6;

     l_index := a_arr.first;
     while (l_index is not null)
     loop
         keys_.extend();
         keys_(keys_.count):=l_index;
         dbms_output.put_line(keys_(keys_.count));
         l_index := a_arr.next(l_index);
     end loop;
end;
/

/*
1
10
100
*/

引用自:https://dba.stackexchange.com/questions/22139