[MS-SQL] 테이블 컬럼 설명과 함께 보기 - GetTableDescription()
Wdev
2014-05-02
MS-SQL을 즐겨쓰는 가장 큰 이유 중 하나가 바로 다이어그램이다.
별도의 적용 없이 DB에 바로 적용이 가능하다는 점이 여러모로 시간을 허비하지 않아서인것 같다.
다이어그램의 테이블에 커스텀모드로 하면 각 컬럼의 설명을 추가 할 수 있다.


▲ MS-SQL 다이아그램 화면

프로젝트의 결과물을 만들때 문서 두께의 상당부분을 차지하는게 DB(Table)명세서 인데, 이것의 작성 시 DB에 존재하는 다이어그램에서 결과물을 만들 수 있으면 시간 절약이 많이 될듯하여 만들었던 SP 이다.

sp_help의 "컬럼설명" 추가 버전이라고 봐도 무방 할것 같다.

처음 작성시는 2000을 기준으로 만들어 졌으나 2008에서는 시스템 테이블 부분이 바뀌어서 2008에 맞게 프로그램을 수정 했다.



syntax: sp_GetTableDescription [테이블명]

* 테이블명 : 설명을 보고자 하는 테이블명, 생략하는 경우 db내의 모든 테이블의 내용이 테이블, 컬럼순서 순으로 나오게 된다
* 테이블명이 생략되면 현재 사용 DB의 전체 테이블 컬럼이 나온다.(문서 작성시 유용)



▲ 실행화면

/*
Prog. by: IKK, Dec-2008
http://www.wdev.co.kr

 update: Jun-2009
  = debug null description
  = except 'sysname' type

 update: Apr-2012
  = for SQL2008 (change system tables)
 
*/
 
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_GetTableDescription]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_GetTableDescription]
GO
 
SET QUOTED_IDENTIFIER ON 
GO

SET ANSI_NULLS ON 
GO

CREATE PROCEDURE sp_GetTableDescription
 @TableName varchar(255) = null
AS
set nocount on
if @TableName is NULL
begin
 select c.id, c.colid as No,
 o.name as TableName,
 c.name as ColumeName,
 t.name as type,
 c.length,
 c.colstat as Identy,
 c.isnullable,
 description = Convert(sql_variant,'')
 into #GTD
 from sys.syscolumns c, sys.sysobjects o, sys.systypes t
 where c.id=o.id and c.xtype=t.xtype and o.Type='U' and not t.name='sysname'
 
 update #GTD set description=p.value from #GTD a, sys.extended_properties p where a.id=p.major_id and a.no=p.minor_id and p.name = 'MS_Description' 

 select TableName, no, ColumeName, type, length, Identy, 
 [null] = case isnullable when 1 then 'not NULL' else 'NULL' end, description
 from #GTD order by id, no

end

else

begin
 select c.id, c.colid as No,
 o.name as TableName,
 c.name as ColumeName,
 t.name as type,
 c.length,
 c.colstat as Identy,
 c.isnullable,
 description = Convert(sql_variant,'')
 into #GTD2
 from sys.syscolumns c, sys.sysobjects o, sys.systypes t
 where c.id=o.id and c.xtype=t.xtype and not t.name='sysname' and o.Name = @TableName

update #GTD2 set description=p.value from #GTD2 a, sys.extended_properties p where a.id=p.major_id and a.no=p.minor_id and p.name = 'MS_Description' 

select no, ColumeName, type, length, Identy,
 [null] = case isnullable when 0 then 'not NULL' else 'NULL' end, description
 from #GTD2 order by no
end
 
return(0)
 
SET QUOTED_IDENTIFIER OFF 
go
 
SET ANSI_NULLS ON 
go

****
 실 서버에선 SQL-injection에서 테이블 정보가 상세하게 노출될 위험이 있으니, 되도록 개발 서버쪽에서만 사용하는 것을 추천 함
저작권자 © WDEV, 무단전재 및 재배포 금지