2013年9月4日水曜日

[SQL Server] SQL Serverでテーブル定義取得(全テーブル)

SQL Serverで、テーブル定義らしいものを取得するクエリです。
これとの違いは、データベース内のすべてのテーブルについて出力します。

  1. select  
  2.  obj.name as 'テーブル名'  
  3. ,col.name as '名称'  
  4. ,(select top 1 name from systypes where systypes.xtype = col.xtype) as '型'  
  5. ,col.length as '桁数'  
  6. ,case isnull(col.scale,0)  
  7.  when 0 then ' '  
  8.  else cast( col.scale as char(10) )  
  9.  end '小数部'  
  10. ,case  col.isnullable  
  11.  when 0 then '○'  
  12.  else ' '  
  13.  end 'nn'  
  14. ,isnull((select case colid when 0 then '' else '○' end  from sysindexkeys as keys where col.id = keys.id  and col.colid = keys.colid and keys.indid = 1),' 'as 'pk'  
  15. ,isnull((select top 1 ex.value from sys.extended_properties as ex where col.id = ex.major_id and ex.minor_id = col.colid and ex.name = 'ms_description' ),' 'as 'コメント'  
  16. from syscolumns as col  
  17. inner join sysobjects as obj on col.id = obj.id  
  18. where obj.name in  (select name from sysobjects where xtype = 'u')