SQLでSQLServerのデータディクショナリ情報を取得する方法

2017.11.26

新しいプロジェクトに参加したり、データベース内の項目を調べたりするときに便利な方法です。

テーブル定義書がメンテナンスされていないときにも使えます。

SQL でテーブルの情報を取得してエクセルなどでまとめる。

この SQL を実行してデータベース内のテーブル情報が取得できる。

select
	t1.xtype as TableType		-- テーブルの型(U:テーブル、V:ビュー)
	,t1.name as TableName		-- テーブル名
	,t2.name as ColumnName		-- 列名
	,t3.name as ColumnTypeName	-- データ型
	,t2.length as ColumnLength	-- データの長さ
	,case t2.isnullable when 0 then 'not null' else '' end as ColumnNullOK	--null許可=1
from
	sysobjects as t1
	inner join
	syscolumns as t2
	on t1.id = t2.id
	inner join systypes as t3
	on t2.xtype = t3.xtype
	and t2.xusertype = t3.xusertype
where
	t1.xtype in ('U','V')
order by
	t1.xtype
	,t1.name
	,t2.colorder