• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

How to assign query to a procedure call directly?

I have a function call like this in a program,

where deid, formula,p_dst_fvc_id,p_dst_fvc_id all are variables.

now In the above call, Instead of formula, I need to fetch a value from a query directly
I should not assign the result to a variable and then pass it to the query.

In place of formula , I need to use the query.

formula query =
'select abcid
from abc  
where abcid=123'

Anyway for this.

1 Solution
slightwv (䄆 Netminder) Commented:
I'm not understanding the question.

What are you wanting in the form of results?
Swadhin RaySenior Technical Engineer Commented:

>>>>where deid, formula,p_dst_fvc_id,p_dst_fvc_id all are variables.

I understood this part.
Now you dont want the formula variable but in place of that you want to use a dynamic query ..

So you want to use like :


If you want to use the dynamic query then 1st run the query and then pass the result to your formula variable.

This should solve your problem

You have to define the paramere 'formula' as VARCHAR2.

So you will be able to invoke the procedure so:

SMS.SETDETFORM(DEID,'query = select abcid from abc  where abcid=123',P_DST_FVC_ID,P_DST_FVC_ID);

In the procedure use a block of statements that:

1. Encounter the keyword 'query'
2. Place the character after '=' in a variable
3. run
          execute immediatelly  INTO variable
4. use 'variable' as value returned by the query
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

'select abcid
from abc  
where abcid=123 group by abcid'  --<== critical query only returns 1 row (value)

Swadhin RaySenior Technical Engineer Commented:
For more information on dynamic queries you can follow this link hope this will help you:

Devinder Singh VirdiCommented:
Let me know if I understood correctly:-

create or replace function temp_dev_f1 ( a number, b number) return number as
  if b < 50 then
    return 10;
    return 5;
  end if;

select temp_dev_f1(1,1) from dual;


select temp_dev_f1(1, x) from ( select rownum x from dual connect by level < 100) ;

You will get dynamic value of X from lower query.
sakthikumarAuthor Commented:
Hi for those who didn't understand,

I want something like the below example
Update table B set employee = (select name from Table A where ID = 3) where ID = 3

For employee value, it uses a query to get the value directly. Like this for below,

where deid, formula,p_dst_fvc_id,p_dst_fvc_id all are variables.

SMS.SETDETFORM is procedure for which we need to pass these
as parameters.

What I want is
for the parameter formula, I have a query, this should be used directly in procedure.
Is this possible by any means?

Devinder Singh VirdiCommented:
Try this:-

Update table B set employee = (select SMS.SETDETFORM(DEID, x, ...) from (select name, (select x from another_table t2.x = t1.x) from Table A where ID = 3)) where ID = 3

I have not tested above code.
Mark GeerlingsDatabase AdministratorCommented:
You cannot call a PL\SQL procedure directly in a query.
You can however call a PL\SQL function directly in a query (that is, if the function returns a datatype that the query can handle, like a varchar2, number or date).

So, if you re-write the procedure to be a function, you may be able to call the function from your query.  Or, if you write a "wrapper" function that in turn calls the procedure, you could call the "wrapper' function from your query, and that in turn, will call the procedure.

Or, are you asking about passing in a query to a procedure (or function) and having that query dynamically executed in the procedure (or function)?  That is possible, but remember that Oracle is optimized for static SQL statements (that can use bind variables supplied by input parameters).  If you choose to use dynamic SQL in PL\SQL (which is possible if you use the "execute immediate..." syntax, or the more complex procedures in the DBMS_SQL package) be aware that your performance will be slower, and there may be more security risks.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now