如何在查询结果中用注释替换标题?

时间:2019-06-26 07:09:52

标签: sql oracle

我想查询一个表,该表的字段名称无法识别,例如:akb12ake32 ...

但是字段注释是有意义的,因此我想在查询结果中将标题替换为注释。而且该表太大,无法使用“ AS”来替换。

正常结果:

akb12  |  ake32
value1 |  value2

我想要什么:

akb12's comment | ake32's comment
value1          |   value2        

3 个答案:

答案 0 :(得分:1)

如果注释已经存储在数据库中,则可以查询d = { 'Target_Tab': 'employees', 'Target_Col': 'empp_id last_name first_name', 'Source_Col': 'emp_id l_name f_name', 'Source_Tab': 'employee' } col = ["Source_Tab","Source_Col","Target_Tab","Target_Col"] df = pd.concat([pd.Series(v.split(), name=k) for k, v in d.items()], axis=1).fillna('')[col] print (df) Source_Tab Source_Col Target_Tab Target_Col 0 employee emp_id employees empp_id 1 l_name last_name 2 f_name first_name 。这是表col = ["Source_Tab","Source_Col","Target_Tab","Target_Col"] df = pd.Series(d).str.split(expand=True).fillna('').reindex(col).T print (df) Source_Tab Source_Col Target_Tab Target_Col 0 employee emp_id employees empp_id 1 l_name last_name 2 f_name first_name 的示例,其中某些列已注释:

d =  {
    'Target_Tab': 'employees',
    'Target_Col': 'empp_id  last_name  first_name',
    'Source_Col': 'emp_id    l_name    f_name',
    'Source_Tab': 'employee'
    }

L = ['Source_Tab','Source_Col']
df = (pd.concat([pd.Series(v.split(), name=k) for k, v in d.items() if k in L], axis=1)
        .fillna(''))
print (df)
  Source_Col Source_Tab
0     emp_id   employee
1     l_name           
2     f_name           

我们的表具有三列,其中两列已注释。现在运行此代码块(如果需要,可以将其更改为过程):

user_col_comments

此代码动态创建了一个视图test。您可以查询它:

create table test (a, b, c) as (
  select 1, 'B1', 'C101' from dual union all
  select 2, 'B2', 'C102' from dual );

comment on column test.a is 'Id';
comment on column test.c is 'Category name';

将标头替换为已定义的注释(列A和C)的结果:

declare 
  v_table varchar2(30) := 'TEST';
  v_sql varchar2(4000);
begin
  select listagg(column_name|| case when comments is not null then ' as "'||comments||'"' end,', ')
         within group (order by column_id)
    into v_sql
    from user_tab_columns 
    left join user_col_comments using (table_name, column_name)
    where table_name = v_table;
  v_sql := 'create or replace view vw_'||v_table||'_comments as select '||v_sql||' from '||v_table;
  execute immediate v_sql;
end;

dbfiddle demo

如果要使用其他架构中的表,请使用vw_test_commentsselect * from vw_test_comments; 并在查询中添加 Id B Category name ------ -- ------------- 1 B1 C101 2 B2 C102

答案 1 :(得分:0)

只需将表名选择为别名,即可更改带注释的标题 就像

   Select col1 as comment1 , col2 as comment2 from table 

在select语句中设置别名是简单且正确的方法

答案 2 :(得分:0)

我认为您应该使用动态别名等

 select  c.Name as ColumnName,
 ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS rownum
 from sys.tables t 
 inner join sys.columns c on c.object_id = t.object_id 
 where t.name = 'tableName' 

通过此查询,您将从表中获取columnNames 尝试实施您的查询,如果有帮助,请告诉我

所以你应该使用类似的东西

 Select col1 as  (select  c.Name as ColumnName,
 ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS rownum
 from sys.tables t 
 inner join sys.columns c on c.object_id = t.object_id 
 where t.name = 'tableName' and rownum = @yourrownum) + @yourvariable from table