Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6002277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:57:18+00:00 2026-05-23T00:57:18+00:00

I have two types CREATE OR REPLACE TYPE my_record_type IS OBJECT ( name varchar2(30)

  • 0

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 a function

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 transformation part with something like

v_table = CAST( v_hash as  my_table_type )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:57:19+00:00Added an answer on May 23, 2026 at 12:57 am

    You may use the 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 of a function (either as a stand along type OR in a Package Specification so it will be visible to the SQL Engine) otherwise you will receive 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
    

    have a look here and here for some more examples and whatnot

    timing differences (based on this methodology #s are in hundreths of a second)

    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
    

    (the above time trials were variations based on this code:

    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 ;
    /
    

    Thus the loop fill is 50% faster, but the time difference is still minuscule (here is the balance between premature optimization and avoiding something because it may be too long, I would recommend doing time trials on your real data to find the solution that best fits).

    The only other ‘elegant’ solution I can think of is TREAT, but you’ll notice it requires a subtype/supertype solution that must be on an object type (I couldn’t get it to work on an Varray/Assoc. Array type — hopefully I’m wrong!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, quick question. I have a table of messages. There are two types
I have a cocoa app with two types windows each of which requires a
I have two implementations of a method, one for value types and another for
I have two expressions of type Expression<Func<T, bool>> and I want to take to
We have two Tables: Document: id, title, document_type_id, showon_id DocumentType: id, name Relationship: DocumentType
A common (i assume?) type of query: I have two tables ('products', 'productsales'). Each
I have two identical tables and need to copy rows from table to another.
Suppose that I have two data types Foo and Bar. Foo has fields x
I am trying to create a WCF Streaming Service. I have two requirements that
I have two applications written in Java that communicate with each other using XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.