[답변]테이블ID 구하는 방법 0 0 1,981

by 백해현 [2007.08.17 11:24:28]



SELECT  DECODE(C.COLUMN_ID, 1, C.TABLE_NAME, NULL)       AS TABLE_NAME
     ,  DECODE(C.COLUMN_ID, 1, B.COMMENTS  , NULL)       AS TABLE_DESC
     ,  C.COLUMN_ID                 AS COLUMN_ID
     ,  C.COLUMN_NAME                AS COLUMN_NAME
     ,  SUBSTR(D.COMMENTS,1,120)              AS COLUMN_DESC
     ,  E.PRIMARY_KEY                AS PRIMARY_KEY
     , (CASE
         WHEN (CASE            
                      WHEN DATA_TYPE IN ('VARCHAR2','CHAR') THEN TO_CHAR(DATA_LENGTH)
                WHEN DATA_SCALE IS NULL OR DATA_SCALE = 0 THEN TO_CHAR(DATA_PRECISION)
                ELSE TO_CHAR(DATA_PRECISION) || ',' || TO_CHAR(DATA_SCALE)
            END) IS NULL THEN C.DATA_TYPE
            ELSE  C.DATA_TYPE || '(' ||     
              (CASE            
                      WHEN DATA_TYPE IN ('VARCHAR2','CHAR') THEN TO_CHAR(DATA_LENGTH)
                WHEN DATA_SCALE IS NULL OR DATA_SCALE = 0 THEN TO_CHAR(DATA_PRECISION)
                ELSE TO_CHAR(DATA_PRECISION) || ',' || TO_CHAR(DATA_SCALE)
            END) || ')'
        END)                               AS DATA_TYPE     
     , C.NULLABLE              AS NULLABLE
  FROM  USER_TABLES       A,
        USER_TAB_COMMENTS B,
        USER_TAB_COLUMNS  C,
        USER_COL_COMMENTS D,
        (SELECT TABLE_NAME  AS TABLE_NAME  ,
                COLUMN_NAME AS COLUMN_NAME ,
                POSITION    AS PRIMARY_KEY
           FROM USER_CONS_COLUMNS
          WHERE (TABLE_NAME,
                 CONSTRAINT_NAME) IN (SELECT TABLE_NAME,
                                             CONSTRAINT_NAME
                                        FROM USER_CONSTRAINTS
                                       WHERE TABLE_NAME      =  DECODE(:TABLE_NAME ,NULL, TABLE_NAME, :TABLE_NAME)
                                         AND CONSTRAINT_TYPE = 'P')
        ) E
 WHERE  A.TABLE_NAME   =  B.TABLE_NAME
   AND  B.TABLE_TYPE   = 'TABLE'
   AND  B.TABLE_NAME   =  C.TABLE_NAME
   AND  C.TABLE_NAME   =  D.TABLE_NAME
   AND  C.COLUMN_NAME  =  D.COLUMN_NAME
   AND  C.TABLE_NAME   =  E.TABLE_NAME (+)
   AND  C.COLUMN_NAME  =  E.COLUMN_NAME(+)
   AND  C.TABLE_NAME   =  DECODE(:TABLE_NAME ,NULL, C.TABLE_NAME, :TABLE_NAME)
 ORDER  BY C.TABLE_NAME
         , B.COMMENTS
         , C.COLUMN_ID
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입