3
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 1 year has passed since last update.

SQL Server で DB 内の全テーブルのカラム情報を一覧化する

Last updated at Posted at 2021-03-18

自分用覚え書き。

SELECT
    tbl.name AS 'TableName',
    c.name AS 'ColumnName',
	ty.name AS 'Type',
	c.max_length AS 'MaxLength',
	c.is_nullable AS 'IsNullable',
    ep.value AS 'Description',
    c.encryption_type_desc AS 'EncryptionType'
FROM
    sys.tables tbl
LEFT JOIN
    sys.columns c
    ON
        c.object_id = tbl.object_id
LEFT JOIN
    sys.types ty
    ON
        ty.user_type_id = c.user_type_id
LEFT JOIN
    sys.extended_properties ep
    ON
        ep.major_id = c.object_id
        AND
        ep.minor_id = c.column_id
ORDER BY
    tbl.name, c.name

急に「データベース定義どうなってる?ざっくりでいいんで出して」とか言われたときに 「接続情報教えましょうか」とか「ソースコード見れば分かります」とか言いたい気持ちを抑えて さくっと実行する用。

更新履歴

2021/03/21: Description が無くても結果に含まれるように修正。
2021/06/18: カラム名を第2ソートに追加。
2021/10/19: 暗号化列が分かるように EncryptionType を SELECT に追加。
2023/03/15: カラム最大長と NULL 許可かどうかを追加。

3
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
3
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?