2012-06-06 13 views
19

Chcę tabeli desc tabeli ; operacja, aby wyświetlić komentarze kolumn. Widziałem, że niektórzy ludzie to osiągnęli, jednak nie mogłem się dowiedzieć, jak to zrobić. Może to zależy od wersji SQL Developer, moja jest 2.1.0.63. Baza danych to Oracle 11g.Jak wyświetlić komentarze kolumny z operacją desc

To jest to, co otrzymuję, robiąc desc table;:

Desc table; 
    Name    Nullable Type 
    ------------------- -------- ----- 
    ID     NOT NULL NUMBER(38) 
    ITEM_ID      NUMBER(38) 

I chciałbym, żeby coś takiego:

Desc table; 
    Name    Nullable Type  Comment 
    ------------------- -------- ---------- --------------------------------- 
    ID     NOT NULL NUMBER(38) Table's id 
    ITEM_ID      NUMBER(38) Reference to an item 

Odpowiedz

24

polecenie desc jest interpretowane w różny sposób dla różnych narzędzi. To, co robi, to wybierz kilka standardowych widoków Oracle.

Oto zapytanie o te widoki, które będą zawierały pożądane dane kolumn, ale zachęcam do zrobienia wyboru *, aby zobaczyć wszystko, co jest dostępne.

Masz 3 typy widoków, dba_ , all_ i widoki user_ *. Używam user_ *, ponieważ jest on dostępny dla każdego schematu/użytkownika, ale wyświetla tylko obiekty należące do tego schematu/użytkownika. Widoki dba_ są zwykle dostępne tylko dla dba, a widoki all-view mogą być lub mogą być niedostępne w zależności od tego, ile dba ci zaufała.^_^

select tc.column_name 
,  tc.nullable 
,  tc.data_type || case when tc.data_type = 'NUMBER' and tc.data_precision is not null then '(' || tc.data_precision || ',' || tc.data_scale || ')' 
          when tc.data_type like '%CHAR%' then '(' || tc.data_length || ')' 
          else null 
         end type 
,  cc.comments 
from user_col_comments cc 
join user_tab_columns tc on cc.column_name = tc.column_name 
          and cc.table_name = tc.table_name 
where cc.table_name = upper(:tablename) 
+1

Ben edytowany mój kod, stwierdzając, że nie wszystkie kolumny mają komentarz i należy użyć sprzężenie zewnętrzne, ale wszystkie kolumny mają rekord w widoku user_col_comments – winkbrace

+0

Cóż, nie jest to dokładnie to, co chciałem, ale jest wystarczająco dobre. Dzięki! :) – Guito

4

Oto definicja z Oracle SQL Developer (jak pokazano w widoku kolumny tabeli):

SELECT "COLUMN_NAME", "DATA_TYPE", "NULLABLE", "DATA_DEFAULT", "COLUMN_ID", "COMMENTS" FROM(
select c.column_name, case when data_type = 'CHAR'  then  data_type||'('||c.char_length||decode(char_used,'B',' BYTE','C',' CHAR',null)||')'  
          when data_type = 'VARCHAR' then  data_type||'('||c.char_length||decode(char_used,'B',' BYTE','C',' CHAR',null)||')'  
          when data_type = 'VARCHAR2' then  data_type||'('||c.char_length||decode(char_used,'B',' BYTE','C',' CHAR',null)||')'  
          when data_type = 'NCHAR' then  data_type||'('||c.char_length||decode(char_used,'B',' BYTE','C',' CHAR',null)||')'  
          when data_type = 'NUMBER' then  
            case when c.data_precision is null and c.data_scale is null then   'NUMBER' 
            when c.data_precision is null and c.data_scale is not null then   'NUMBER(38,'||c.data_scale||')' 
            else   data_type||'('||c.data_precision||','||c.data_SCALE||')'  end  
          when data_type = 'NVARCHAR' then  data_type||'('||c.char_length||decode(char_used,'B',' BYTE','C',' CHAR',null)||')'  
          when data_type = 'NVARCHAR2' then  data_type||'('||c.char_length||decode(char_used,'B',' BYTE','C',' CHAR',null)||')'  
          else  data_type end data_type, 
    decode(nullable,'Y','Yes','No') nullable, 
c.DATA_DEFAULT,column_id, com.comments   
    from sys.Dba_tab_Columns c, 
     sys.Dba_col_comments com 
    where c.owner  = :OBJECT_OWNER 
    and c.table_name = :OBJECT_NAME 
    and c.table_name = com.table_name 
    and c.owner = com.owner 
    and c.column_name = com.column_name     
    order by column_id 
) 
Powiązane problemy