如果要写代码生成器,肯定会需要查询表字段与字段的注释。不然生成的代码还需要很多手动的操作。但由于各类数据库的系统表结构不一样,因此针对不同类型的查询sql也是不一样的。
oracle:
SELECT A.TABLE_NAME,A.COMMENTS,B.COLUMN_NAME,B.COMMENTS FROM USER_TAB_COMMENTS A,USER_COL_COMMENTS B WHERE A.TABLE_NAME=B.TABLE_NAME and a.table_name='SYS_TIME'
sqlserver2000:
select sc.name as columnName,sp.value as remarks from sysobjects so left outer join syscolumns sc on so.id = sc.id left outer join sysproperties sp on sc.id = sp.id and sc.colid = sp.smallid where so.type = 'u' and so.name='$tableName$' order by so.id, sc.colorder
sqlserver2005:
SELECT columnName=A.NAME, remarks=ISNULL(G.[VALUE], ' ') 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='$tableName$' ORDER BY A.ID,A.COLORDER
sqlserver2008:
SELECT a.name columnName, ISNULL(g.value,'') AS remarks FROM syscolumns a LEFT JOIN systypes b ON a.xtype=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 dbo.sysproperties g
ON d.id=g.id AND a.colid = g.smallid WHERE d.name='$tableName$' ORDER BY a.id,a.colorder
mysql:
select table_name,table_comment from information_schema.tables where table_schema = 'db' and table_name ='tablename'