如何查找SQL中使用的所有表和列名?它在ORACLE数据库上。下面是一个SQL示例。
SELECT
A.ENAME,
A.AGE as EMP_AGE,
B.DNAME
FROM
emp a,
dept b
WHERE
a.deptno= b.deptno
我希望输出像这样
TABLENAME, COLUMNNAME
EMP, ENAME
EMP, DEPTNO
EMP, AGE
DEPT, DNAME
DEPT, DEPTNO
我做了一些研究但未能找到完美的解决方案。如果我们创建一个视图或存储过程会有帮助吗?请指教。
答案 0 :(得分:1)
我有一个很好的解决方案,但您需要做两件事:
将SQL放在PL / SQL程序单元中。所以,是的,你提到的存储过程。
在12.2实例上编译该程序单元和所有相关表(即,安装应用程序代码)(您可以在http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html下载12.2,或者您可以在云上购买Exadata Express CLoud服务。 oracle.com或获得300美元的积分,可以在cloud.oracle.com/tryit上免费使用一个月。
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)
希望有所帮助!
在livesql.oracle.com上有更多PL / Scope的例子。只需搜索" pl / scope" (杜)。
答案 1 :(得分:0)
我所意识到的是,此解决方案不会为您提供预计的列,因此可能并不完全需要您;它只提供谓词中使用的列
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#
执行计划将为您提供谓词信息。例如,
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]
答案 2 :(得分:-1)
如果您可以将查询转换为VIEW,然后使用INFORMATION_SCHEMA.VIEW_COLUMN_USAGE
以下是一个例子:
让我们说你的观点名称是ABC
然后使用这个
SELECT VIEW_NAME, TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.VIEW_COLUMN_USAGE
WHERE VIEW_NAME = 'ABC'
让我知道它是否有效..