0
0

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】テーブル定義を取得

Posted at

【SQL】テーブル定義を取得

WITH BASE as(		
	  SELECT CONCAT (TABLE_SCHEMA, '.', TABLE_NAME)as TABLE_NAME		
	    , COLUMN_NAME		
		, DATA_TYPE	
		, CHARACTER_MAXIMUM_LENGTH	
		, CHARACTER_OCTET_LENGTH	
		, NUMERIC_PRECISION	
		, NUMERIC_PRECISION_RADIX	
		, DATETIME_PRECISION	
		, IIF(IS_NULLABLE = 'YES', ',', 'NotNull') AS NN	
		, ORDINAL_POSITION	
	  FROM [INFORMATION_SCHEMA].[COLUMNS]		
	  WHERE TABLE_NAME IN ('{ テーブル物理名 }')		
	)		
	SELECT TABLE_NAME AS 'テーブル名'		
	  , COLUMN_NAME AS 'カラム名'		
	  , DATA_TYPE AS '型'		
	  , CASE		
	      WHEN DATA_TYPE in ('char', 'nchar', 'nvarchar', 'varchar') THEN		
		    IIF(CHARACTER_MAXIMUM_LENGTH > 0, CHARACTER_MAXIMUM_LENGTH, '')	
	      WHEN DATA_TYPE in ('bigint', 'int', 'tinyint') THEN		
		    IIF(NUMERIC_PRECISION > 0, NUMERIC_PRECISION, '')	
		  WHEN DATA_TYPE in ('numeric', 'decimal') THEN	
		    IIF(NUMERIC_PRECISION > 0, NUMERIC_PRECISION, '')	
		    --IIF(NUMERIC_PRECISION > 0, CONCAT(NUMERIC_PRECISION, NUMERIC_SCALE) , '')	
			
		  WHEN DATA_TYPE in ('bit', 'varbinary', 'date', 'datetime') THEN ''	
			
			ELSE ''
		END AS 桁	
	  , NN AS NotNull		
	FROM BASE		
	ORDER BY TABLE_NAME, ORDINAL_POSITION		
	;
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?