반응형

오라클을 사용하다 보면 테이블이나 컬럼들의 목록을 조회 해야할 때가 있습니다.


1. 테이블 목록과 코멘트 조회 쿼리 입니다.


1
2
3
4
5
6
7
8
SELECT
    b.table_name,
    b.comments
FROM
    user_tables a
    LEFT OUTER JOIN all_tab_comments b ON a.table_name = b.table_name
WHERE
    b.table_type = 'TABLE';



2. 컬럼 목록과 코멘트 조회 쿼리 입니다. (조금 길군요.)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
SELECT
    tab_columns.table_name,
    tab.comments AS tablecomment,
    tab_columns.column_id,
    tab_columns.column_name,
    (
        CASE
            WHEN data_type LIKE '%CHAR%' THEN data_type
             || '('
             || data_length
             || ')'
            WHEN
                data_type = 'NUMBER'
            AND
                data_precision > 0
            AND
                data_scale > 0
            THEN data_type
             || '('
             || data_precision
             || ','
             || data_scale
             || ')'
            WHEN
                data_type = 'NUMBER'
            AND
                data_precision > 0
            THEN data_type
             || '('
             || data_precision
             || ')'
            WHEN data_type = 'NUMBER' THEN data_type
            ELSE data_type
        END
    ) data_type,
    DECODE(
        nullable,
        'N',
        'Not Null',
        'Null'
    ) nullable,
    data_default,
    (
        SELECT
            DECODE(
                SUM( (
                    SELECT
                        DECODE(
                            constraint_type,
                            'P',
                            1,
                            'R',
                            2,
                            0
                        )
                    FROM
                        user_constraints
                    WHERE
                        constraint_name = cons_columns.constraint_name
                ) ),
                1,
                'PK',
                2,
                'FK',
                3,
                'PK,FK',
                ''
            )
        FROM
            user_cons_columns cons_columns
        WHERE
                table_name = tab_columns.table_name
            AND
                column_name = tab_columns.column_name
    ) constraints,
    comments.comments
FROM
    user_tab_columns tab_columns,
    user_col_comments comments,
    all_tab_comments tab
WHERE
        tab_columns.table_name = comments.table_name (+)
    AND
        tab_columns.table_name = tab.table_name
    AND
        tab_columns.column_name = comments.column_name (+)
ORDER BY
    tab_columns.table_name,
    column_id



3. 2번 쿼리 조회결과 입니다.


반응형

+ Recent posts