MSSQL 데이터베이스에서 순번, 칼럼코드, 칼럼명, 타입, IS NULL 까지 조회할 수 있는 쿼리

Posted by MD워시퍼
2012. 8. 5. 11:32 Study/MS-SQL
728x90
SELECT b.column_id AS COLUMN_ID

      , b.name AS COLUMN_NAME
      , c.value AS DESCRIPTION
      , CASE WHEN d.CHARACTER_MAXIMUM_LENGTH > 0 THEN d.DATA_TYPE + '(' + CAST(d.CHARACTER_MAXIMUM_LENGTH as VARCHAR)+ ')' ELSE d.DATA_TYPE END
      , d.IS_NULLABLE
      , a.name AS TABLE_NAME
   FROM sys.objects a
    LEFT JOIN sys.columns b ON a.object_id=b.object_id
    LEFT JOIN sys.extended_properties c ON (a.object_id=c.major_id AND b.column_id=c.minor_id)
    LEFT JOIN INFORMATION_SCHEMA.COLUMNS d ON (a.name=d.TABLE_NAME AND b.name=d.COLUMN_NAME)
   ORDER BY a.name, b.column_id;


DB 설계서 제작시 유용함(농심 프로젝트시에 사용함)