Search notes:

Oracle: DBA_SOURCE

DBA_SOURCE is a data dictionary view which allows to query the source code of PL/SQL objects.
DBA_SOURCE comes in four variants: USER_SOURCE, ALL_SOURCE, DBA_SOURCE and CDB_SOURCE.

Select source-code of a given PL/SQL Package, Procedure or Function, or MLE Module

The following query shows the source code of a given PL/SQL object
Using the condition line between … only selects the source lines in a given range, so that, hopefully, the user does not have to scroll through a long result set:
select
   line,
   text
from
   dba_source
where
   line between    5 and 15        and
   owner         =  user           and
-- name          = 'STANDARD'      and
   name          = 'TQ84_PKG_B'    and
   type          in ('PACKAGE BODY', 'TYPE BODY', 'PROCEDURE', 'FUNCTION')
-- origin_con_id = 0
order by
   line;
Github repository Oracle-Patterns, path: /Installed/data-dictionary/source/show-package-bodys-source-text.sql
With the introduction of MLE modules in 23c, the source of such modules can be found with where type = 'MLE MODULE'.

Search for source code text lines with a given text

The following example searches for source lines that match a regular expression (regexp_like):
select
   owner,
   src.name,
   src.line,
   src.text,
   src.type
from
   dba_source src
where
   src.owner not in (select username from dba_users where oracle_maintained = 'Y') and
   regexp_like(src.text, 'create.*table', 'i')
order by
   src.owner,
   src.name,
   src.type;

Search for N lines before and M lines after matched source code line

The previous select statement finds exactly those source code lines that match a regular expression. Oftentimes, I find this not really satisfactory because I'd like to see a matched line's preceding M and following N lines.
This is possible with analytic functions, but it somewhat blows up the size of the SQL statement:
with find_matches as (
   select
      owner, name, line, text, type,
      case when regexp_like(text,
      ----------------------------------
      -- Regexp to search for goes here:
      --
        'return clob', 'i'
      --
      ----------------------------------
      ) then 1 end match
   from
      dba_source
   where
      owner not in (select username from dba_users where oracle_maintained = 'Y')
),
partitions as (
   select
      owner, name, line, text, type, match,
      sum(match) over(partition by owner, name, type order by line  asc) partition_match_asc,
      sum(match) over(partition by owner, name, type order by line desc) partition_match_desc
   from
      find_matches
),
row_numbers as (
   select
      owner, name, line, text, type, match,
      row_number() over (partition by owner, name, type, partition_match_asc  order by line  asc ) rn_asc,
      row_number() over (partition by owner, name, type, partition_match_desc order by line  desc) rn_desc,
      partition_match_asc,
      partition_match_desc
   from
      partitions
)
select
   case when match = 1 then '*' end match,
   owner, name, line, text, type
-- rn_asc, rn_desc
from
   row_numbers
where
   (partition_match_asc  is not null and rn_asc  <= 3) or -- Preceding n lines
   (partition_match_desc is not null and rn_desc <= 4)    -- Following  m lines
order by
   owner, name, type, line;

See also

The names of functions and procedures found in PL/SQL packages and types can be selected from dba_procedures.
Errors in source code is recorded in dba_errors etc.
dba_source does not store the source of tables or views.
Column-names of tables and views can be queried from dba_tab_columns.
Alternatively, the definition of tables can be queried using the package dbms_metadata.
The select statement of a view can be queried from dba_views.
Information about identifiers in PL/SQL source code can be queried from dba_identifiers
The error message ORA-06512: at , line N reveals object, line and position of the statement in the PL/SQL source where an exception was thrown.
The SQL script getcode.sql extracts the source code from PL/SQL object and stores it in a file.

Index