Skip to Main Content
  • Questions
  • Identifying PL/SQL function call from SQL

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, michal.

Asked: January 23, 2018 - 7:35 am UTC

Last updated: January 29, 2018 - 1:30 pm UTC

Version: 11.2.0.4 ( or 12.2)

Viewed 1000+ times

You Asked

Hello,
Firstly, thanks for fantastic job You're doing - i (and i hope many others) really appreciate it.

I wanted to ask if there is an automated way to identify PL/SQL functions called from SQL.
I would like to prepare list of "candidates" for using PRAGMA UDF in 12c, but i'm out of ideas how to search for them.

Thanks in advance,
kind regards

and Chris said...

In the general case, no, I'm not aware of a way to find this. Other than scraping the v$ views.

If you're on 12.2 you can use PL/Scope to inspect SQL within your PL/SQL that calls other PL/SQL.

alter session set plscope_settings='identifiers:all, statements:all'
/
create or replace function f ( p int ) 
  return int as
begin
  return p + 1;
end f;
/

create or replace procedure p ( param int ) as
  val int;
begin
  select f(param)
  into   val
  from   dual;
  
  dbms_output.put_line(val);
end p;
/

exec p(1);

with my_prog_unit as (
  select user owner, 'P' object_name from dual
), full_set as (
  select ai.usage,
         ai.usage_id,
         ai.usage_context_id,
         ai.type,
         ai.name
  from   all_identifiers ai, my_prog_unit
  where  ai.object_name = my_prog_unit.object_name
  and    ai.owner = my_prog_unit.owner
  union all
  select st.type,
         st.usage_id,
         st.usage_context_id,
         'TYPE',
         'NAME'
  from   all_statements st, my_prog_unit
  where  st.object_name = my_prog_unit.object_name
  and    st.owner = my_prog_unit.owner
), dml_statements as (
  select st.owner, st.object_name, st.line, st.usage_id, st.type
  from   all_statements st, my_prog_unit
  where  st.object_name = my_prog_unit.object_name
  and st.owner = my_prog_unit.owner
  and st.type in ('SELECT', 'UPDATE', 'DELETE')
)
  select st.owner,
         st.object_name,
         st.line,
         st.type,
         s.text
  from   dml_statements st, all_source s
  where ('CALL', 'FUNCTION') in (
    select fs.usage, fs.type
    from   full_set fs
    connect by prior fs.usage_id = fs.usage_context_id
    start with fs.usage_id = st.usage_id
  )
  and st.line = s.line
  and st.object_name = s.name
  and st.owner = s.owner;

OWNER   OBJECT_NAME   LINE   TYPE     TEXT                 
CHRIS   P                  4 SELECT     select f(param)


HT to Steven Feuerstein for the SQL statement above. He talks more about using PL/Scope to find PL/SQL in SQL in PL/SQL at: https://stevenfeuersteinonplsql.blogspot.co.uk/2017/06/more-122-plscope-magic-find-sql.html

Rating

  (1 rating)

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

Comments

michal klenczon, January 29, 2018 - 6:46 am UTC

Hello Chris,

I've came with some "not that elegant" solution based on v$ views, but this PL/Scope base solution is very nice.

Thanks again to You ( and Steven Feuerstein )

kind regards,
michal
Chris Saxon
January 29, 2018 - 1:30 pm UTC

No problem!

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