Skip to Main Content
  • Questions
  • Getting calling where clause or select statement in pl/sql function

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Olivier.

Asked: August 29, 2018 - 8:37 pm UTC

Last updated: September 04, 2018 - 5:37 am UTC

Version: 11g

Viewed 1000+ times

You Asked

Hi there !

Is there a way of getting the select statement that called a pl/sql function or its where clause ?

For instance, if the call is :

SELECT customFunction(parameter) AS FuncResult, field1 FROM table WHERE field2 = 'test'

Is it possible to retrive either the complete select statement (SELECT customFunction(parameter) AS FuncResult, field1 FROM table WHERE field2 = 'test') or the where clause (field2 = 'test') in the pl/sql code of customFunction() ?

As you may have deducted, I don't control/build the where clause. Another software (the caller) is doing it so I cannot change/alter it.

Thanks


and Connor said...

Not that I know of. There is a bit of a catch-22 here, because to probe for that SQL we must *run* SQL, which of course then becomes the currently running SQL, eg

SQL> create or replace
  2  function blah(c int) return int is
  3    l_sql_id1 varchar2(30);
  4    l_sql_id2 varchar2(30);
  5    l_sql_text varchar2(1000);
  6  begin
  7    select sql_id, prev_sql_id
  8    into   l_sql_id1, l_sql_id2
  9    from   v$session
 10    where  sid = sys_context('USERENV','SID');
 11
 12    dbms_output.put_line(l_sql_id1);
 13    dbms_output.put_line(l_sql_id2);
 14
 15    return c;
 16  end;
 17  /

Function created.

SQL>
SQL> set serverout on
SQL> select blah(10) from dual;

  BLAH(10)
----------
        10

fu3ng5ux16xn2
5t10uu7v11s5t
SQL> select sql_id, sql_text
  2  from   v$sql
  3  where  sql_id in
  4  ( 'fu3ng5ux16xn2','5t10uu7v11s5t');

SQL_ID        SQL_TEXT
------------- ----------------------------------------------------------------
5t10uu7v11s5t BEGIN DBMS_OUTPUT.ENABLE(NULL); END;
fu3ng5ux16xn2 SELECT SQL_ID, PREV_SQL_ID FROM V$SESSION WHERE SID = SYS_CONTEX
              T('USERENV','SID')




Do you need it at run time (ie, within the function) or are you just trying to detect excessive calls etc. You could do some data mining of v$sqlstats to detect executions, rows processed, etc

Even if you *could* pick it up, I'd have concerns because you generally do not want to be aggressively querying v$ structures, so even my example above would not be good if you were doing (say)

select blah(object_id) from all_objects;

because I'd be smashing v$session tens of thousands of times in the one call.

Rating

  (3 ratings)

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

Comments

Very useful and complete

Olivier, August 31, 2018 - 12:52 pm UTC

Connor,

thanks for your quick and complete answer.

Meanwhile I came to the same conclusion : The SQL is running so I can't take any actions on it.

My research lead me to another way to achieve my goal so I'll ask another question.

Thanks again!


Very useful and complete

Olivier, August 31, 2018 - 2:39 pm UTC

Connor,

thanks for your quick and complete answer.

Meanwhile I came to the same conclusion : The SQL is running so I can't take any actions on it.

A colleague lead me to another path to maybe achieve my goal.

The goal is to return fieldX or fieldY depending on the where clause (which I don't have any control on).

I think the TABLE() function is very close to what I want to achieve : select * from TABLE(customFunction()) where param1=XXX AND param2=XXX ...

That way I construct the output in customFunction() thus enabling me to return a field or another depending on the where clause parameters.

This approach leads me to two other aspects :

-Since I need to create a TYPE for this to work, is it possible to create it dynamically using the actual table schema (where the data will be coming from) at runtime ? Everything is there so it might be possible but I don't know if Oracle supports this.

-I would also need to get the actual values (WHERE field1=value1 AND field2=value2) of the where clause to perform the verification.

Thanks for your help and let me know if you consider this as other questions.

Useful and fast

Olivier, August 31, 2018 - 2:45 pm UTC

Connor,

thanks for your quick and complete answer.

Meanwhile I came to the same conclusion : The SQL is running so I can't take any actions on it.

A colleague lead me to another path to maybe achieve my goal.

The goal is to return fieldX or fieldY depending on the where clause (which I don't have any control on).

I think the TABLE() function is very close to what I want to achieve : select * from TABLE(customFunction()) where param1=XXX AND param2=XXX ...

That way I construct the output in customFunction() thus enabling me to return a field or another depending on the where clause parameters.

This approach leads me to two other aspects :

-Since I need to create a TYPE for this to work, is it possible to create it dynamically using the actual table schema (where the data will be coming from) at runtime ? Everything is there so it might be possible but I don't know if Oracle supports this.

-I would also need to get the actual values (WHERE field1=value1 AND field2=value2) of the where clause to perform the verification.

Thanks for your help and let me know if you consider this as other questions.
Connor McDonald
September 04, 2018 - 5:37 am UTC

I think I'd need to see a top to bottom test case, because I'm not sure what you *do* have control over and what you do *not* have control over.


More to Explore

Performance

Get all the information about database performance in the Database Performance guide.