zoukankan      html  css  js  c++  java
  • postgresql 查询表注释 和 字段注释

    表注释:

    with tmp_tab as (
        select pc.oid as ooid,pn.nspname,pc.*
          from pg_class pc
               left outer join pg_namespace pn
                            on pc.relnamespace = pn.oid
          where 1=1
           and pc.relkind in ('r','v','m','f','p')
           and pn.nspname not in ('pg_catalog','information_schema') -- select pn.oid, pn.* from pg_namespace pn where 1=1
           and pn.nspname not like 'pg_toast%'
           /*and pc.oid not in (
              select inhrelid
                from pg_inherits
           )*/
        order by pc.relname
    ),tmp_desc as (
       select pd.*
         from pg_description pd
        where 1=1
          and pd.objsubid = 0 --objsubid 对于一个表列上的一个注释,这里是列号(objoid和classoid指表本身)。对所有其他对象类型,此列为0。
          --and pd.objoid=168605
    )
    select t0.*,
           case when t0.relkind in ('r','p')
                     then 'comment on table '||t0.nspname||'.'||t0.relname||' is '''||coalesce(t0.description,'')||''';' 
                when t0.relkind='v' 
                     then 'comment on view '||t0.nspname||'.'||t0.relname||' is '''||coalesce(t0.description,'')||''';' 
            end as table_description
      from (
            select tab.nspname,
                   tab.relname,
                   tab.relkind,
                   de.description
              from tmp_tab tab
                   left outer join tmp_desc de
                                on tab.ooid = de.objoid 
             where 1=1    
            ) t0
     where 1=1
       and t0.description like '%教师%'
    order by t0.nspname,t0.relname   
    ;

    字段注释:

    with tmp_tab as (
        select pc.oid as ooid,pn.nspname,pc.*
          from pg_class pc
               left outer join pg_namespace pn
                            on pc.relnamespace = pn.oid
          where 1=1
           and pc.relkind in ('r','v','m','f','p')
           and pn.nspname not in ('pg_catalog','information_schema') -- select pn.oid, pn.* from pg_namespace pn where 1=1
           and pn.nspname not like 'pg_toast%'
           /*
           and pc.oid not in (
              select inhrelid
                from pg_inherits
           )*/
        order by pc.relname
    ),tmp_col as (
       select pa.*
         from pg_attribute pa
        where 1=1
          --and pa.attrelid = 168605
          and pa.attisdropped = false
          and pa.attname not in (
          'tableoid',
          'cmax',
          'xmax',
          'cmin',
          'xmin',
          'ctid'
          )
    ),tmp_desc as (
       select pd.*
         from pg_description pd
        where 1=1
          and pd.objsubid <> 0 --objsubid 对于一个表列上的一个注释,这里是列号(objoid和classoid指表本身)。对所有其他对象类型,此列为0。
          --and pd.objoid=168605
    )
    select t0.*,
           'comment on COLUMN '||t0.nspname||'.'||t0.relname||'.'||t0.attname||' is '''||coalesce(t0.description,'')||''';' as column_description
      from (
            select tab.nspname,
                   tab.relname,
                   tc.attname,
                   tc.attnum,
                   de.description
              from tmp_tab tab
                   left outer join tmp_col tc
                                on tab.ooid = tc.attrelid
                   left outer join tmp_desc de
                                on tc.attrelid = de.objoid and tc.attnum = de.objsubid
           ) t0
     where 1=1
       --and t0.description like '%教师%'
    order by t0.nspname,t0.relname, t0.attnum
    桂棹兮兰桨,击空明兮溯流光。
  • 相关阅读:
    selector
    c# word 转pdf 导出失败,因为此功能尚未安装
    Jquery
    Enter
    获取当前地址的参数值
    无法向会话状态服务器发出会话状态请求。
    Ajax 跨域请求
    JsonResult 处理时间格式
    VS2013打开项目提示"Asp.net4.5未在web服务器上注册
    死锁
  • 原文地址:https://www.cnblogs.com/nanfei/p/15336483.html
Copyright © 2011-2022 走看看