0
2

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.

【SQLServer】データベース内の指定したカラム名を持つテーブルおよび詳細情報の検索

Last updated at Posted at 2022-02-04

カラム名からデータ型だったり有効桁数だったり確認したいなーって時のコピペ用に。

select
	t.name as TableName
    , c.name as ColumnName
    , type_name(user_type_id) as 属性
	, c.precision as 有効桁数 
	, c.scale as 小数点以下桁数 
from sys.tables as t
	inner join sys.columns as c
		on t.object_id = c.object_id
where c.name = 'hoge' -- 任意のカラム名
order by TableName, ColumnName
;

テーブル定義書に必要な項目名、データ型なんかをとってきたーい!
そんなときのためのクエリもついでにおいておきます。

select
	c.name as ColumnName
    , type_name(user_type_id) as 属性
    , max_length as 長さ
	, c.precision as 有効桁数 
	, c.scale as 小数点以下桁数 
    , case when is_nullable = 1 then 'YES' else 'NO' end as NULL許可
from sys.tables as t
	inner join sys.columns as c
		on t.object_id = c.object_id
where t.name = 'fuga' -- 取得したいテーブル名
order by ColumnName
;

その他に取りたい要素があった場合の参考
https://docs.microsoft.com/ja-jp/sql/relational-databases/system-catalog-views/sys-columns-transact-sql?view=sql-server-ver15

0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?