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
If you can convert your query into VIEW and then use INFORMATION_SCHEMA.VIEW_COLUMN_USAGE
Here is an example:
let's say your view name is ABC
then use this one
SELECT VIEW_NAME, TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.VIEW_COLUMN_USAGE
WHERE VIEW_NAME = 'ABC'
Let me know if it works..
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]
I have a great solution for you, but there are two things you will need to do:
Place the SQL inside a PL/SQL program unit. So, yes, to the stored procedure you mentioned.
Compile that program unit and all dependent tables (that is, install your application code) on a 12.2 instance (you can download 12.2 at http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html or you can purchase an Exadata Express CLoud Service at cloud.oracle.com or get a $300 credit to use one at no cost for a month at cloud.oracle.com/tryit).
12.2 is key because the feature you REALLY want to use is called PL/Scope and it is a compiler tool that collections information about PL/SQL identifiers (as of 11.1) and SQL usage inside PL/'SQL (as of 12.2).
CREATE TABLE my_data (n NUMBER)
/
ALTER SESSION SET plscope_settings='identifiers:all, statements:all'
/
CREATE OR REPLACE PROCEDURE my_procedure (n_in IN NUMBER)
AUTHID DEFINER
IS
l_n my_data.n%TYPE;
CURSOR all_data_cur
IS
SELECT *
FROM my_data
FOR UPDATE OF n;
BEGIN
INSERT INTO my_data (n)
VALUES (n_in);
END;
/
SELECT idt.line,
idt.owner || '.' || idt.object_name code_unit,
idt.name column_name,
RTRIM (src.text, CHR (10)) text
FROM all_identifiers idt, all_source src
WHERE idt.usage = 'REFERENCE'
AND idt.TYPE = 'COLUMN'
AND idt.line = src.line
AND idt.object_name = src.name
AND idt.owner = src.owner
AND idt.object_name = 'MY_PROCEDURE'
ORDER BY code_unit, line
/
LINE CODE_UNIT COLUMN_NAME TEXT
4 STEVEN.MY_PROCEDURE N l_n my_data.n%TYPE;
10 STEVEN.MY_PROCEDURE N FOR UPDATE OF n;
12 STEVEN.MY_PROCEDURE N INSERT INTO my_data (n)
Hope that helps!
Lots more examples of PL/Scope at livesql.oracle.com. Just search for "pl/scope" (duh).