An elegant way to convert an index from a table to a simple table in PL / SQL, and then loop through each record

I have two types

CREATE OR REPLACE TYPE my_record_type IS OBJECT
  (
    name        varchar2(30)
  )
  ;

CREATE OR REPLACE TYPE my_table_type AS TABLE OF my_record_type

and functions

create or replace my_function return my_table_type
is
  type my_hash_type is table of my_record_type index by pls_integer;
  v_hash my_hash_type;
  v_table my_table_type;
  i NUMBER;
begin
 -- some business logic here

 -- transformation part

 v_table := my_table_type();
 i := v_hash.first;

 while i is not null loop

  v_table.extend(1);
  v_table(v_table.last) := v_hash(i);
  i := v_hash.next(i); 

 end loop;

 --  end transformation part

 return v_table;
end;
/

Is there an elegant way in 10g to replace the conversion part with something like

v_table = CAST( v_hash as  my_table_type )
+3
source share
1 answer

You can use SELECT my_record_type(column_value) BULK COLLECT INTO v_table from table(v_hash). But in order to use this, you will have to create my_hash_type outside the function (either as a rack of type OR in the package specification so that it is visible to SQL Engine), otherwise you will get a PLS-00642: local collection types not allowed in SQL statements.

CREATE OR REPLACE TYPE my_hash_type is table OF VARCHAR2(10);
/
set serveroutput on
declare 
 --type my_hash_type is table OF VARCHAR2(10);
  v_hash my_hash_type := my_hash_type();
  v_table my_table_type;
  i NUMBER;
begin
null ;
  for n in 60..75 loop
    V_hash.extend(1);  
    V_hash(v_hash.count) := chr(n) ;
  end loop ;

  select my_record_type(column_value)
  bulk collect into  v_table
  from table(v_hash) ;

  for n in 1..v_table.count loop
    dbms_output.put_line( n || ':>' || v_table(n).name);
  end loop ;

  --PLS-00642: local collection types not allowed in SQL statements

end ;

1:><
2:>=
3:>>
4:>?
5:>@
6:>A
7:>B
8:>C
9:>D
10:>E
11:>F
12:>G
13:>H
14:>I
15:>J
16:>K

look here and here for a few more examples and whatnot

( # hundreths )

pl/sql context switch (as described above)
44
42
43
42

loop fill (with type defined outside of block) --A distinct CREATE TYPE on Oracle level

18
18
18
18

loop fill (with type defined within block) --Type created within the Anon. block
23
22
24
22

( :

set serveroutput on
declare 
 --type my_hash_type  is table of my_record_type -index by pls_integer;
  v_hash my_hash_type := my_hash_type();
  v_table my_table_type;
  i NUMBER;
  time_before BINARY_INTEGER; 
  time_after BINARY_INTEGER;
begin

time_before := DBMS_UTILITY.GET_TIME; 

  for n in 0..15000 loop
    V_hash.extend(1);  
    V_hash(v_hash.count) := my_record_type(n) ;
  end loop ;


  select my_record_type(column_value)
  bulk collect into  v_table
  from table(v_hash) ;


  /*
  v_table := my_table_type();
  for n in 1..V_hash.count loop
    v_table.extend(1);
    v_table(v_table.count) := v_hash(n) ;
    --dbms_output.put_line( n || ':>' || v_table(n).name);
  end loop ;*/
  --for n in 1..v_table.count loop
  --  dbms_output.put_line( n || ':>' || v_table(n).name);
  --end loop ;
time_after := DBMS_UTILITY.GET_TIME; 

DBMS_OUTPUT.PUT_LINE (time_after - time_before);
  --PLS-00642: local collection types not allowed in SQL statements

end ;
/

, 50% , - ( -, , , , ).

"" , , TREAT, , /, ( Varray/Assoc. Array - , !)

+4

All Articles