How can I find the underlying column and table name for an Oracle view?

前端 未结 7 1837
余生分开走
余生分开走 2021-01-23 05:34

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 col

相关标签:
7条回答
  • 2021-01-23 05:48

    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.

    0 讨论(0)
  • 2021-01-23 05:51

    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?

    0 讨论(0)
  • 2021-01-23 05:53

    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.

    0 讨论(0)
  • 2021-01-23 05:56

    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'
    
    0 讨论(0)
  • 2021-01-23 06:05

    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');
    
    0 讨论(0)
  • 2021-01-23 06:07

    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
    
    0 讨论(0)
提交回复
热议问题