Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
视图列注释中的SQL表列注释_Sql_Oracle_Sql View - Fatal编程技术网

视图列注释中的SQL表列注释

视图列注释中的SQL表列注释,sql,oracle,sql-view,Sql,Oracle,Sql View,我有一个表,其中列注释解释了每列的用途。我知道我可以通过查询所有列注释来查看这些列注释 但是我想基于此表创建一个视图,并且希望此视图中的列继承原始表中的列注释 select comm.table_name, comm.column_name, comm.comments from all_col_comments comm join all_tab_cols cols on comm.table_name=cols.table_name and comm.owner=cols.owner

我有一个表,其中列注释解释了每列的用途。我知道我可以通过查询所有列注释来查看这些列注释

但是我想基于此表创建一个视图,并且希望此视图中的列继承原始表中的列注释

select comm.table_name, comm.column_name, comm.comments   
from all_col_comments comm
join all_tab_cols cols on comm.table_name=cols.table_name and comm.owner=cols.owner and comm.column_name=cols.column_name
where comm.owner = :OWNER
and comm.table_name = 'PERSON'
order by comm.table_name, cols.column_id;
结果是:

TABLE_NAME  COLUMN_NAME COMMENTS
PERSON      PERS_ID     PERS_ID is the identifier of a person.
PERSON      LANG_ID     LANG_ID identifies the person's language.
现在,如果我基于此表创建视图,我必须手动添加注释。我发现的最好的方法是将其硬编码到CREATEVIEW语句中,这是一种非常糟糕的方法。一定有更好的办法

create table person (id number, col1 varchar2(50 char));
comment on column person.id is 'it is ID';
comment on column person.col1 is 'it is COL1';

create or replace view v_person as select id from person;
您可以尝试以下查询:

select ud.name view_name, 
       ud.referenced_name based_tab,
       vcols.column_name,
       nvl(vcom.comments, tcom.comments) comments
from all_dependencies ud
join all_tab_columns vcols on vcols.table_name = ud.name and vcols.owner=ud.owner
left join all_tab_columns tcols on tcols.table_name = ud.referenced_name and vcols.column_name = tcols.column_name and tcols.owner=ud.owner
left join all_col_comments tcom on ud.referenced_name = tcom.table_name and tcols.column_name = tcom.column_name and tcom.owner=ud.owner
left join all_col_comments vcom on ud.name = vcom.table_name and vcols.column_name = vcom.column_name and vcom.owner=ud.owner
where ud.name = 'V_PERSON' 
  and ud.type = 'VIEW' 
  and ud.referenced_type = 'TABLE';
使用所有_依赖项查找视图所基于的表

两次将其与所有_tab_列联接,以查找有关视图列和表列的信息。如果视图的列名与基础表的列名相同,则此查询将起作用

左键连接所有列注释两次,以查找视图和表列注释


当Oracle提供所有列注释时,为什么有一个单独的列注释表?还是我完全误解了你的问题?基于此表的视图您的意思是创建或替换视图V_Person作为Select*from Person?为什么不简单地从表的ddl脚本复制并粘贴注释,然后用V_Person搜索并替换Person?或者我理解错了什么?我必须在这个查询中添加一点内容,以便在每个联接中包含OWNER列,因为两个模式中都存在一个同名的视图。除此之外,工作起来很有魅力!