find column names and table names referenced in SQL

前端 未结 3 1126
春和景丽
春和景丽 2021-01-07 03:32

How to find all the tables and column names used in a SQL? It is on ORACLE database. Below is an SQL example.

SELECT 
   A.ENAME,
   A.AGE as EMP_AGE,
   B         


        
3条回答
  •  粉色の甜心
    2021-01-07 04:05

    What I have realised is that this solution will not give you the projected columns, so it may not totally need you needs; it only gives you the columns used in predicates

    select
      r.name owner
    , o.name tabl 
    , c.name colmn
    from
    sys.col_usage$ u,
    sys.obj$ o,
    sys.col$ c,
    sys.user$ r
    where
    r.name='&scahme' and
    o.obj# = u.obj#
    and c.obj# = u.obj#
    and c.col# = u.intcol#
    and o.owner# = r.user#
    

    The execution plan will give you predicate information. For example,

    SQL> select ename as name, job as junk
      2  from emp
      3  /
    
     select * from table( dbms_xplan.display_cursor( null, null, 'ADVANCED' ) )
    
    Plan hash value: 3956160932
    
    --------------------------------------------------------------------------
    | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    --------------------------------------------------------------------------
    |   0 | SELECT STATEMENT  |      |       |       |     2 (100)|          |
    |   1 |  TABLE ACCESS FULL| EMP  |    14 |   196 |     2   (0)| 00:00:01 |
    --------------------------------------------------------------------------
    
    Query Block Name / Object Alias (identified by operation id):
    -------------------------------------------------------------
    
       1 - SEL$1 / EMP@SEL$1
    
    Outline Data
    -------------
    
      /*+
          BEGIN_OUTLINE_DATA
          IGNORE_OPTIM_EMBEDDED_HINTS
          OPTIMIZER_FEATURES_ENABLE('12.1.0.2')
          DB_VERSION('12.1.0.2')
          ALL_ROWS
          OUTLINE_LEAF(@"SEL$1")
          FULL(@"SEL$1" "EMP"@"SEL$1")
          END_OUTLINE_DATA
      */
    
    Column Projection Information (identified by operation id):
    -----------------------------------------------------------
    
       1 - "ENAME"[VARCHAR2,10], "JOB"[VARCHAR2,9]
    

提交回复
热议问题