Search code examples
sqloracle-databaseplsqlnotepad++

How extract table names from PL/SQL package body file?


I need to get the table names queried in a pl/sql package file.

I know that there is an option for this in Notepad++ by regex but I don't know what regex to apply for get the table names (I understand that must be some regex to take the keyword "FROM" and get the next string after space, I think so).

For the next example code:

CREATE OR REPLACE PACKAGE BODY pac_example AS

FUNCTION f1 RETURN NUMBER IS
BEGIN
  SELECT * FROM table1;
  RETURN 1;
END f1;

FUNCTION f2 RETURN NUMBER IS
BEGIN
  SELECT * FROM table2;
  RETURN 1;
END f2;

END pac_example;

And I expect replace all and get the file with only its table names:

table1
table2

Solution

    • Ctrl+H
    • Find what: (?:\A(?:(?!FROM).)*|\G)FROM\s+(\w+(?:\s*,\s*\w+)*)(?:(?!FROM).)*
    • Replace with: " #a space and a double quote
    • check Wrap around
    • check Regular expression
    • CHECK . matches newline
    • Replace all

    Explanation:

    (?:                 # start non capture group
      \A                # beginning of file
      (?:(?!FROM).)*    # Tempered greedy token, make sure we haven't FROM before
     |                  # OR
      \G                # restart from last match position
    )                   # end group
    FROM\s+             # literally FROM followed by 1 or more spaces
    (                   # start group 1
      \w+               # 1 or more word characters (table name)
      (?:\s*,\s*\w+)*   # non capture group spaces comma spaces and 1 or more word characters, optional more tables
    )                   # end group
    (?:(?!FROM).)*      # Tempered greedy token, make sure we haven't FROM 
    

    Replacement:

    $1          # content of group 1, table name
    

    Screen capture:

    enter image description here