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 8120743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:14:53+00:00 2026-06-06T05:14:53+00:00

I have a question about stored procedures in Oracle. Below is the stored procedure

  • 0

I have a question about stored procedures in Oracle.
Below is the stored procedure and tables as it stands:

create table STORES
(
    ID number,
    NAME varchar2(100),
    CITY varchar2(100),
    EXPIRES DATE
)

insert into stores values(1, 'Store 1', 'City 1', sysdate);
insert into stores values(2, 'Store 2', 'City 1', sysdate);
insert into stores values(3, 'Store 3', 'City 2', sysdate);

create table CLOSED
(
    ID number,
    NAME varchar2(100),
    CITY varchar2(100)
)

create or replace PROCEDURE 
pr_TestProc(subQuery  IN VARCHAR2)
IS
begin
    insert into CLOSED (ID, NAME, CITY) 
    select ID, NAME, CITY 
    from STORES 
    where ID in (1, 2, 3);
end;

What I’d like to do is replace the “in” values with the subQuery passed in as a parameter.
So if I run the procedure like:

execute pr_TestProc('select ID from STORES where EXPIRES <= sysdate');

The query passed in should be executed as a subquery being run inside the procedure.
Something like:

insert into CLOSED (ID, NAME, CITY) select ID, NAME, CITY 
from STORES 
where ID in (execute(subQuery));

Obviously this doesn’t work, but what would be the best way to achieve this, or is it even possible?

Thanks,
Brian

  • 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-06-06T05:14:56+00:00Added an answer on June 6, 2026 at 5:14 am

    You can use dynamic SQL

     create or replace PROCEDURE pr_TestProc(subQuery  IN VARCHAR2)
     IS
       l_sql_stmt varchar2(1000);
     begin
       l_sql_stmt := 'insert into CLOSED (ID, NAME, CITY) ' ||
                     ' select ID, NAME, CITY ' ||
                     '   from STORES ' ||
                     '  where id in (' || subquery || ')';
       dbms_output.put_line( l_sql_stmt );
       EXECUTE IMMEDIATE l_sql_stmt;
     end;
    
    SQL> execute pr_TestProc('select ID from STORES where EXPIRES <= sysdate');
    
    PL/SQL procedure successfully completed.
    
    SQL> column name format a20
    SQL> column city format a20
    SQL> select * from closed;
    
            ID NAME                 CITY
    ---------- -------------------- --------------------
             1 Store 1              City 1
             2 Store 2              City 1
             3 Store 3              City 2
    

    If you are calling this procedure rarely in a system that is relatively idle, that will probably work acceptably well. If you are calling it frequently with different subqueries, however, you are going to generate a ton of non-sharable SQL statements. That will force Oracle to do a lot of hard parsing. It will also flood your shared pool with non-sharable SQL statements, likely forcing out plans that you want to be cached, forcing more hard parsing when those SQL statements are then executed again. And if you do it fast enough, you’re likely to end up getting errors (or causing other processes to get errors) that Oracle couldn’t allocate enough memory in the shared pool for a particular query. Plus, dynamic SQL is harder to write, harder to debug, vulnerable to SQL injection attacks, etc. so it generally makes the system harder to deal with.

    A more elegant solution would be to pass in a collection rather than a subquery would be to pass in a collection

    SQL> create type id_coll
      2      as table of number;
      3  /
    
    Type created.
    
    
    SQL> ed
    Wrote file afiedt.buf
    
      1  create or replace PROCEDURE pr_TestProc( p_ids IN id_coll)
      2  is
      3  begin
      4      insert into CLOSED (ID, NAME, CITY)
      5      select ID, NAME, CITY
      6      from STORES
      7      where ID in (select column_value
      8                     from table( p_ids ) );
      9* end;
    SQL> /
    
    Procedure created.
    
    SQL> ed
    Wrote file afiedt.buf
    
      1  declare
      2    l_ids id_coll;
      3  begin
      4    select id
      5      bulk collect into l_ids
      6      from stores
      7     where expires <= sysdate;
      8    pr_TestProc( l_ids );
      9* end;
    SQL> /
    
    PL/SQL procedure successfully completed.
    
    SQL> select * from closed;
    
            ID NAME                 CITY
    ---------- -------------------- --------------------
             1 Store 1              City 1
             2 Store 2              City 1
             3 Store 3              City 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A simple question about Stored Procedures. I have one stored procedure collecting a whole
I have a question about inserting column stored procedure into table. I have two
I have a question about a performance of stored procedures in the ADS. I
Trying to get started with subsonic but have a simple question about stored procedures
Have a question about stored procedure: How to retrieve the variable's value from stored
I have a question about the in parameter type of a stored procedure. Normally
I have a small question about stored procedures and the DateTime2 datatype in SQL
I have read these very good questions on SO about SQL stored procedures: When
I have a question about User-Defined Table Types in SQL Server 2008. For the
i have a question about the reasonableness of using entity framework only with stored

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.