2012-11-13 42 views
0

我被要求創建一個基本上與describe命令做同樣事情的腳本。 我已經想出瞭如何編寫腳本並獲得我想要的輸出,但第二部分的任務是「複製describe命令的格式」我很難過,是否真的有一種方法可以簡單地複製該格式?Oracle:類似於描述命令的腳本

回答

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. 
+0

這實際上正是我在想什麼,但我不確定是否有某種方式來複制確切的格式只是想確保我沒有錯過一些想要獲得儘可能多的權利笑哈謝謝! – user1801794