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
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]