2013年9月4日水曜日

[SQL Server] SQL Serverでテーブル定義取得(全テーブル)

SQL Serverで、テーブル定義らしいものを取得するクエリです。
これとの違いは、データベース内のすべてのテーブルについて出力します。

select
 obj.name as 'テーブル名'
,col.name as '名称'
,(select top 1 name from systypes where systypes.xtype = col.xtype) as '型'
,col.length as '桁数'
,case isnull(col.scale,0)
 when 0 then ' '
 else cast( col.scale as char(10) )
 end '小数部'
,case  col.isnullable
 when 0 then '○'
 else ' '
 end 'nn'
,isnull((select case colid when 0 then '' else '○' end  from sysindexkeys as keys where col.id = keys.id  and col.colid = keys.colid and keys.indid = 1),' ') as 'pk'
,isnull((select top 1 ex.value from sys.extended_properties as ex where col.id = ex.major_id and ex.minor_id = col.colid and ex.name = 'ms_description' ),' ') as 'コメント'
from syscolumns as col
inner join sysobjects as obj on col.id = obj.id
where obj.name in  (select name from sysobjects where xtype = 'u')