Skip to Main Content
  • Questions
  • Using select statement to pass single result to stored procedure

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Mencio.

Asked: September 07, 2017 - 3:31 pm UTC

Last updated: September 08, 2017 - 1:28 am UTC

Version: 11.2.0.4

Viewed 1000+ times

You Asked

Hi,

I know this is a simple one but yet I hasn't been able to find an answer anywhere.

All I want to do is to use a simple query as parameter in an EXEC when calling a stored procedure.

Something like this:

EXEC MY_PACKAGE.MY_PROCEDURE(select TRUNC(MAX(DATE_I_WANT)) FROM MY_TABLE)

MY_PACKAGE.MY_PROCEDURE expects an argument of DATE type.

Is it possible or do I need to create a temp variable to store DATE_I_WANT before I call the procedure?

thanks.

and Connor said...

Yep, you'll need a temp variable.

There are certain circumstances where you can pass SQL in, for example, functions called from SQL and pipelined functions

SQL> create or replace function take_a_date(p_date date) return number is
  2  begin
  3    return 1;
  4  end;
  5  /

Function created.

SQL>
SQL> select take_a_date((select sysdate from dual))
  2  from dual;

TAKE_A_DATE((SELECTSYSDATEFROMDUAL))
------------------------------------
                                   1

SQL>
SQL> create or replace function take_a_date(p_datecur sys_refcursor) return sys.odcinumberlist pipelined is
  2  begin
  3    pipe row ( 1 );
  4    return;
  5  end;
  6  /

Function created.

SQL>
SQL> select * from table(take_a_date(cursor(select sysdate v from dual)));

COLUMN_VALUE
------------
           1


but a straight procedure call is not one of those.

Is this answer out of date? If it is, please let us know via a Comment

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library