1. 程式人生 > 實用技巧 >Java 資料庫查詢表字段,欄位型別,欄位註釋

Java 資料庫查詢表字段,欄位型別,欄位註釋

Java 資料庫查詢表字段,欄位型別,欄位註釋

  MySQL資料庫:
  SELECT
      table_name AS 'table_name',
      column_name AS 'table_field',
      column_type AS 'field_type',
      column_comment AS 'field_name'
  FROM
      information_schema.COLUMNS
  WHERE
      table_name = '表名稱';

  ============================================================

  SqlServer資料庫:
  SELECT
      table_name= cast(d.name as varchar(32)),
      table_field=CAST(a.name as varchar(32)),
      field_type=CAST(b.name as varchar(32)),
      field_name=CAST(isnull(g.[value],'') as varchar(32))
  FROM syscolumns   a
      left join systypes b on a.xusertype=b.xusertype 
      inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name <> 'dtproperties' 
      left join syscomments e on a.cdefault=e.id 
      left join sys.extended_properties g on a.id=g.major_id and a.colid=g.minor_id 
      left join sys.extended_properties f on d.id=f.major_id and f.minor_id=0 
  where 
      d.name='表名稱'     
  order by 
      a.id,a.colorder