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

  • SEARCH
  • Home
  • 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 4045956
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:30:04+00:00 2026-05-20T13:30:04+00:00

Is it possible to create a dynamic SQL statement that pulls from an existing

  • 0

Is it possible to create a dynamic SQL statement that pulls from an existing collection?

l_collection := pack.get_items(
                i_code => get_items_list.i_code ,
                i_name => get_items_list.i_name );

Now, let’s say I want to select a COUNT from that collection using dynamic SQL. Is that possible? Furthermore, I want to do a sub select from that collection as well.

  • 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-20T13:30:04+00:00Added an answer on May 20, 2026 at 1:30 pm

    If the collection type is declared at the schema level, it can be used in SQL statements, including dynamic ones. You need to explicitly cast it to the proper collection type, or the SQL engine has no idea what type it is.

    EXECUTE IMMEDIATE
      'SELECT COUNT(*) FROM TABLE(CAST(:collection AS collection_type))'
       INTO l_count
       USING l_collection
      ;
    

    I’m not sure if there’s some other reason you want to use dynamic SQL, or if you’re just assuming that it’s necessary in this case. It shouldn’t be necessary if all you want to do is select the count. This inline SQL should work fine:

    SELECT COUNT(*) INTO l_count FROM TABLE(CAST(l_collection AS collection_type));
    

    Of course, if that’s all you want you don’t need SQL at all, just l_count := l_collection.COUNT.

    Edit — adding fully worked out example

    CREATE OR REPLACE TYPE testtype AS OBJECT( x NUMBER, y NUMBER);
    /
    
    CREATE OR REPLACE TYPE testtypetab AS TABLE OF testtype;
    /
    
    DECLARE
      t  testtypetab := testtypetab();
      l_count integer;
    BEGIN
      -- Populate the collection with some data
      SELECT testtype(LEVEL, LEVEL) BULK COLLECT INTO t FROM dual CONNECT BY LEVEL<21;
    
      -- Show that we can query it using inline SQL
      SELECT count(*) INTO l_count FROM TABLE(CAST(t AS testtypetab));
      dbms_output.put_line( l_count );
    
      -- Clear the collection
      t.DELETE;
    
      -- Show that we can query it using dynamic SQL
      EXECUTE IMMEDIATE 'select count(*) from table(cast(:collection as testtypetab))'
        into l_count using t;
      dbms_output.put_line( l_count );
    END;
    /
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am asking myself if it is possible to somehow create a dynamic gui
Is possible create an extension for SQL Management Studio in Visual Studio 2010? Visual
I'm attempting to create a T-SQL case statement to filter a query based on
Is it possible to create a view (not stored procedure) with dynamic column names
I have create a LINQ-to-SQL project in Visual Studio 2010 using Dynamic Data. In
Is it possible to create a dynamic context menu in android? What I mean
Is it possible to create a custom Web front-end to run SSRS reports from?
I have a form that has all the fields pulled dynamic from a database
Is it possible to create dynamic HTML page with Javascript ? Now or tomorrow...
Possible Duplicate: Is there a way to create a SQL Server function to "join"

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.