2007年6月8日金曜日

[SQL Server 2005] SQL Serverでテーブル定義取得

SQL Server 2005で、テーブル定義らしいものを取得するクエリです。
これを使って、テーブル定義書を自動生成させてます。

  1. select  
  2. Col.name as '名称'  
  3. ,(Select top 1 name From systypes Where systypes.xtype = Col.xtype) as '型'  
  4. ,Col.length as '桁数'  
  5. ,case isnull(Col.scale,0)  
  6.  when 0 then ' '  
  7.  else cast( Col.scale as char(10) )  
  8.  end '小数部'  
  9. ,case  Col.isnullable  
  10.  when 0 then '○'  
  11.  else ' '  
  12.  end 'NN'  
  13. ,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'  
  14. ,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 'コメント'  
  15. From syscolumns as col  
  16. inner join sysobjects as obj on col.id = obj.id  
  17. Where obj.name = '<テーブル名>'  

2 件のコメント:

匿名 さんのコメント...

参考になりました~

匿名 さんのコメント...

非常に参考になりました!
ありがとうございます。