Oracle:类似于describe命令的脚本

时间:2012-11-13 19:43:18

标签: oracle11g sqlplus data-dictionary

我被要求创建一个基本上与describe命令相同的脚本。 我已经想出了如何编写脚本并获得我想要的输出,但是赋值的第二部分是“复制describe命令的格式”我很难过,有没有办法简单地复制那种格式?

1 个答案:

答案 0 :(得分:0)

假设您的查询类似(请注意,我并不打算处理每种不同的数据类型)

select column_name "Name",
       (case when nullable = 'N'
             then 'NOT NULL'
             else null
         end) "Null?",
       (case when data_type = 'DATE'
             then data_type
             when data_type = 'NUMBER' and data_scale > 0
             then data_type || '(' || data_precision || ',' || data_scale || ')'
             when data_type = 'NUMBER' and data_scale = 0
             then data_type || '(' || data_precision || ')'
             when data_type = 'VARCHAR2'
             then data_type || '(' || data_length || ')'
        end) "Type"
  from dba_tab_cols
 where table_name = 'EMP'
 order by column_id

您可以发出一些SQL * Plus命令来格式化输出

SQL> column "Nmae" format a30;
SQL> column "Null?" format a8;
SQL> column "Type" format a30;

然后查询的输出将与DESCRIBE命令的输出

匹配
SQL> ed
Wrote file afiedt.buf

  1  select column_name "Name",
  2         (case when nullable = 'N'
  3               then 'NOT NULL'
  4               else null
  5           end) "Null?",
  6         (case when data_type = 'DATE'
  7               then data_type
  8               when data_type = 'NUMBER' and data_scale > 0
  9               then data_type || '(' || data_precision || ',' || data_scale || ')'
 10               when data_type = 'NUMBER' and data_scale = 0
 11               then data_type || '(' || data_precision || ')'
 12               when data_type = 'VARCHAR2'
 13               then data_type || '(' || data_length || ')'
 14          end) "Type"
 15    from dba_tab_cols
 16   where table_name = 'EMP'
 17*  order by column_id
SQL> /

Name                           Null?    Type
------------------------------ -------- ------------------------------
EMPNO                          NOT NULL NUMBER(4)
ENAME                                   VARCHAR2(10)
JOB                                     VARCHAR2(9)
MGR                                     NUMBER(4)
HIREDATE                                DATE
SAL                                     NUMBER(7,2)
COMM                                    NUMBER(7,2)
DEPTNO                                  NUMBER(2)

8 rows selected.