2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SQL serverでのテーブル定義をクエリで調べる

Posted at

SSMSでテーブル定義を調べるのが面倒!!

SSMSことSQL server Management Studioはとても便利です。
マウスだけで基本的に操作が完結できるので、
SQL初心者にとっても非常に敷居の低いSQLクライアントとなっています。

テーブル定義も「デザイン」から、閲覧&編集できるのもとても重宝します。
ただし時々この設定をエクスポートしたい欲求に駆られます。
日常的にクエリで調べる癖がついていないので、
メモ代わりにクエリでテーブル定義を抽出する方法を残します。

テーブル名を指定して、クエリを実行するだけ

変数@TABLENAMEにテーブル名を書き換えて、
クエリを実行してください。
そのテーブルの列名やデータ型や桁数などを表示します。

DECLARE @TABLENAME NVARCHAR(50)
SET @TABLENAME = 'テーブル名' --テーブル名を指定してください。

SELECT c.column_id AS ID
	,t.name AS テーブル名
	,t.type AS タイプ
	,c.name AS 列名
	,n.name AS データ型
	,c.max_length AS 桁数
	,c.scale AS 小数点桁数
	,c.is_nullable AS 'NULL可否'
	,t.create_date AS 作成日
	,t.modify_date AS 更新日
	FROM sys.objects t
inner join sys.columns c on t.object_id = c.object_id
inner join sys.types n on c.system_type_id = n.system_type_id
where t.type = 'U' AND t.name = @TABLENAME
order by t.name, c.column_id;

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?