如何找到 Oracle 视图的基础列和表名称?

发布于 2024-09-07 01:39:47 字数 445 浏览 4 评论 0原文

这听起来应该很简单,但事实并非如此!我找不到任何 Oracle(元数据)视图可以提供 Oracle 视图列的基础列和表名称。我发现这样做的唯一方法是解析视图源 SQL(这与精确的科学相去甚远)。

为了解释我想要什么,请考虑我在 SCOTT 模式中创建的以下示例视图:

CREATE OR REPLACE VIEW EMP_DEP
(
   EMPLOYEE_NAME,
   DEPARTMENT_NAME
)
AS
SELECT 
   ENAME,
   DNAME 
FROM
   emp a,
   dept b
WHERE
   a.deptno= b.deptno
/

现在给定视图和列名称 EMP_DEP.DEPARTMENT_NAME,我想获取视图使用的基础表和列名称 DEPT.DNAME。有谁知道不涉及解析视图 SQL 的获取此信息的方法吗?

This sounds like it should be simple to do but not so! There is no Oracle (meta-data) view that I can find that gives the underlying column and table name for an Oracle view column. The only way I have found of doing it is to parse the view source SQL (which is far from and exact science).

Just to explain what I want, consider the following example view which I created in SCOTT schema:

CREATE OR REPLACE VIEW EMP_DEP
(
   EMPLOYEE_NAME,
   DEPARTMENT_NAME
)
AS
SELECT 
   ENAME,
   DNAME 
FROM
   emp a,
   dept b
WHERE
   a.deptno= b.deptno
/

Now given the view and column name EMP_DEP.DEPARTMENT_NAME, I would like to get the underlying table and column name used by the view which is DEPT.DNAME. Does anyone know of a way to get this information that does not involve parsing the view's SQL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梦中的蝴蝶 2024-09-14 01:39:47

没有办法,因为每个视图列的定义是一个表达式,而不仅仅是一个表列(通常)。例如,您的视图的 SQL 可能是:

SELECT 
   UPPER(ENAME) || 'xxx',
   myfunction(DNAME)
FROM
   emp a,
   dept b
WHERE
   a.deptno= b.deptno

或者

SELECT ename || 'xxx', dname
FROM (
  SELECT 
     UPPER(ENAME) AS ename,
     myfunction(DNAME) AS dname
  FROM
     emp a,
     dept b
  WHERE
     a.deptno= b.deptno
)

您希望在本示例中看到“底层列”是什么?

There isn't a way, because the definition of each view column is an expression, not (in general) merely a table column. For example, your view's SQL could be:

SELECT 
   UPPER(ENAME) || 'xxx',
   myfunction(DNAME)
FROM
   emp a,
   dept b
WHERE
   a.deptno= b.deptno

or perhaps

SELECT ename || 'xxx', dname
FROM (
  SELECT 
     UPPER(ENAME) AS ename,
     myfunction(DNAME) AS dname
  FROM
     emp a,
     dept b
  WHERE
     a.deptno= b.deptno
)

What would you expect to see for the "underlying columns" in this example?

迷雾森÷林ヴ 2024-09-14 01:39:47

可以使用查询选择用于创建视图的表:

select 
  name , 
  type , 
  referenced_name , 
  referenced_type
from 
  user_dependencies 
where 
  name = 'VIEW_NAME' and 
  type = 'VIEW' and  
  referenced_type = 'TABLE';

如果视图列与表列具有相同的列名,请尝试以下查询:

select 
  distinct table_name, column_name 
from 
  all_tab_columns 
where table_name in (select
                        referenced_name
                      from 
                        user_dependencies 
                      where 
                        name = 'VIEW_NAME' and 
                        type = 'VIEW' and  
                        referenced_type = 'TABLE') 
 and column_name in (select 
                        column_name 
                     from 
                        all_tab_columns 
                     where 
                        table_name = 'VIEW_NAME');

Tables used to created the view can be selected using the query:

select 
  name , 
  type , 
  referenced_name , 
  referenced_type
from 
  user_dependencies 
where 
  name = 'VIEW_NAME' and 
  type = 'VIEW' and  
  referenced_type = 'TABLE';

If view columns have the same column name of table column, then please try the below query:

select 
  distinct table_name, column_name 
from 
  all_tab_columns 
where table_name in (select
                        referenced_name
                      from 
                        user_dependencies 
                      where 
                        name = 'VIEW_NAME' and 
                        type = 'VIEW' and  
                        referenced_type = 'TABLE') 
 and column_name in (select 
                        column_name 
                     from 
                        all_tab_columns 
                     where 
                        table_name = 'VIEW_NAME');
仲春光 2024-09-14 01:39:47

由于请求者正在寻找其表的任何实例,而不是针对特定视图,因此我建议:

SELECT *
  FROM DBA_DEPENDENCIES
 WHERE TYPE = 'VIEW'
   AND REFERENCED_TYPE = 'TABLE'
   AND REFERENCED_NAME = '<TABLE_NAME>'

Since the requester is looking for any instance of his table, and not with respect to a specific view, I would recommend:

SELECT *
  FROM DBA_DEPENDENCIES
 WHERE TYPE = 'VIEW'
   AND REFERENCED_TYPE = 'TABLE'
   AND REFERENCED_NAME = '<TABLE_NAME>'
时光与爱终年不遇 2024-09-14 01:39:47

定义视图的 sql 可以在 all_views 中找到,

set long 9999 
select TEXT from all_views where VIEW_NAME='MYVIEW';

这是获取基础表和列的唯一方法。

The sql that defined the view can be found in all_views

set long 9999 
select TEXT from all_views where VIEW_NAME='MYVIEW';

This is the only way to get at the underlying tables and columns.

醉城メ夜风 2024-09-14 01:39:47

Oracle 在 11g 中引入了 更细粒度的依赖跟踪。因此数据库知道视图或包主体依赖于哪些表列。然而,他们似乎没有在视图中公开这些数据。但可能有 x$ 表包含该信息。

In 11g Oracle introduced finer grained dependency tracking. So the database knows which table columns a view or package body depends on. However, they don't seem to have exposed this data in a view. But there may be x$ tables with the info.

画尸师 2024-09-14 01:39:47

链接过程可能是一些帮助用于识别依赖关系

DBA_DEPENDENCIES 视图将为您提供视图所基于的表的列表:

SELECT *
  FROM DBA_DEPENDENCIES
 WHERE OWNER = <Schema>
   AND NAME = <View_Name>
   AND TYPE = 'VIEW'

The linked procedures may be of some help for identifying dependencies

The DBA_DEPENDENCIES View will give you a list of the tables that a View is based on:

SELECT *
  FROM DBA_DEPENDENCIES
 WHERE OWNER = <Schema>
   AND NAME = <View_Name>
   AND TYPE = 'VIEW'
我一向站在原地 2024-09-14 01:39:47

如果您想要视图表和列依赖关系,这很有效:

WITH view_dependencies (view_name, table_name) AS (
    SELECT CONNECT_BY_ROOT d.name AS view_name, d.referenced_name AS table_name
    FROM all_dependencies d
    WHERE d.referenced_type IN ('TABLE', 'VIEW')
    START WITH d.name = UPPER('jtf_rs_resource_extns_vl') AND d.type = 'VIEW'
    CONNECT BY PRIOR d.referenced_name = d.name AND PRIOR d.referenced_type = d.type
)
SELECT deps.view_name, deps.table_name, tbl.table_id, cols.column_id, 
       cols.column_name  
FROM view_dependencies deps
    LEFT JOIN FND_TABLES tbl ON tbl.table_name = deps.table_name
    LEFT JOIN FND_COLUMNS cols ON tbl.table_id = cols.table_id
ORDER BY deps.view_name,  deps.table_name, cols.column_sequence;

输出

VIEW_NAME                   TABLE_NAME            TABLE_ID COLUMN_ID COLUMN_NAME
=======================================================================================
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563724  RESOURCE_ID
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563712  CREATED_BY
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563713  CREATION_DATE
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563718  LAST_UPDATED_BY
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563719  LAST_UPDATE_DATE
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563720  LAST_UPDATE_LOGIN
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563704  CATEGORY
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563725  RESOURCE_NUMBER
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563729  SOURCE_ID
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563686  ADDRESS_ID
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563709  CONTACT_ID
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563721  MANAGING_EMPLOYEE_ID
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563730  START_DATE_ACTIVE
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563714  END_DATE_ACTIVE
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563732  TIME_ZONE
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563711  COST_PER_HR
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563723  PRIMARY_LANGUAGE
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563726  SECONDARY_LANGUAGE
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563717  IES_AGENT_LOGIN
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563728  SERVER_GROUP_ID
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563687  ASSIGNED_TO_GROUP_ID
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563710  COST_CENTER

This works well if you want a views table and column dependencies:

WITH view_dependencies (view_name, table_name) AS (
    SELECT CONNECT_BY_ROOT d.name AS view_name, d.referenced_name AS table_name
    FROM all_dependencies d
    WHERE d.referenced_type IN ('TABLE', 'VIEW')
    START WITH d.name = UPPER('jtf_rs_resource_extns_vl') AND d.type = 'VIEW'
    CONNECT BY PRIOR d.referenced_name = d.name AND PRIOR d.referenced_type = d.type
)
SELECT deps.view_name, deps.table_name, tbl.table_id, cols.column_id, 
       cols.column_name  
FROM view_dependencies deps
    LEFT JOIN FND_TABLES tbl ON tbl.table_name = deps.table_name
    LEFT JOIN FND_COLUMNS cols ON tbl.table_id = cols.table_id
ORDER BY deps.view_name,  deps.table_name, cols.column_sequence;

Outputs

VIEW_NAME                   TABLE_NAME            TABLE_ID COLUMN_ID COLUMN_NAME
=======================================================================================
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563724  RESOURCE_ID
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563712  CREATED_BY
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563713  CREATION_DATE
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563718  LAST_UPDATED_BY
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563719  LAST_UPDATE_DATE
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563720  LAST_UPDATE_LOGIN
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563704  CATEGORY
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563725  RESOURCE_NUMBER
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563729  SOURCE_ID
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563686  ADDRESS_ID
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563709  CONTACT_ID
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563721  MANAGING_EMPLOYEE_ID
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563730  START_DATE_ACTIVE
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563714  END_DATE_ACTIVE
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563732  TIME_ZONE
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563711  COST_PER_HR
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563723  PRIMARY_LANGUAGE
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563726  SECONDARY_LANGUAGE
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563717  IES_AGENT_LOGIN
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563728  SERVER_GROUP_ID
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563687  ASSIGNED_TO_GROUP_ID
JTF_RS_RESOURCE_EXTNS_VL    JTF_RS_RESOURCE_EXTNS   80056   563710  COST_CENTER
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文