Oracle按表名导出表物理结构Excel形式

可以在PL/SQL工具中打开test窗口进行执行,upper方法中的参数就是需要导出的表名;具体代码如下
declare
  -- Local variables here
  mcomments varchar2(50);
  mField    varchar2(2000);
  mconstraint_type varchar2(500);
  mKey      varchar2(1000);
begin
  for cur in (select t.TABLE_NAME field1
                from user_tables t
               where instr(upper('XS_CPLX,XS_MJLXB,XS_PTXXB,XS_XSSJ,XS_MXPZXX,XS_XCSJB,XS_ZDJBXX'), t.TABLE_NAME) > 0) loop
  
    SELECT nvl((SELECT t.comments
                 FROM user_tab_comments t
                where upper(t.table_name) = upper(cur.field1)),
               '')
      into mcomments
      FROM dual;
  
    dbms_output.put_line(cur.field1 || '(' || mcomments || '):');
  
    dbms_output.put_line('列名' || chr(9) || '数据类型' || chr(9) || '说明' ||
                         chr(9) || '主外键');
  
    for curField in (SELECT *
                       FROM user_tab_columns t
                      where t.TABLE_NAME = upper(cur.field1)
                      order by column_id) loop
    
      SELECT nvl((SELECT t.comments
                   FROM user_col_comments t
                  where upper(t.TABLE_NAME) = upper(cur.field1)
                    and upper(t.column_name) = upper(curfield.column_name)),
                 '')
        into mField
        FROM dual;
    
      SELECT nvl((select max(au.constraint_type)
                   from user_cons_columns cu, user_constraints au
                  where cu.constraint_name = au.constraint_name
                    and cu.column_name = upper(curfield.column_name)
                    and au.table_name = upper(cur.field1)),
                 '')
        into mconstraint_type
        FROM dual;
        mkey:='';
        if(mconstraint_type='P') then
        mkey:='主键';
        end if;
        
        if(mconstraint_type='R') then
        mkey:='外键';
        end if;
    
      dbms_output.put_line(curField.Column_Name || chr(9) || case when
                           curField.Data_Type in ('VARCHAR2') then
                           curField.Data_Type || '(' ||
                           curField.Data_Length || ')' else
                           curField.Data_Type
                           end || chr(9) || replace(mField, chr(10), '') ||
                           chr(9) || mKey);
    end loop;
  
    dbms_output.put_line(chr(13));
    --dbms_output.put_line(chr(10));
  end loop;

end;

平时开发需要用到,所以记录一下。

posted @ 2020-09-27 16:22  lvanka  阅读(387)  评论(0编辑  收藏  举报