1. 数据库信息获取

1.1 获取所有数据库名称

  1. SELECT Name FROM Master..SysDatabases ORDER BY Name

1.2 获取数据库所有表名

  1. select * from sys.tables --获取该数据库下所有表
  2. select * from sysobjects where xtype='u' --获取该数据库下所有表

1.3 获取表所有字段信息

  1. SELECT COLUMN_NAME,DATA_TYPE FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='表名'

1.4 统计表字段个数

  1. select COUNT(a.name) from sys.all_columns a,sys.tables b
  2. where a.object_id=b.object_id and b.name='表名'

2. 数据库表添加注释

2.1 表添加注释信息

2.1.1 表注释信息

  1. EXECUTE sp_addextendedproperty N'MS_Description', N'表注释',
  2. N'user', N'dbo', N'table', N'表名', NULL, NULL

2.1.2 表修改注释信息

  1. execute sp_updateextendedproperty 'MS_Description',
  2. '修改注释-测试表','user','dbo','table','表名',null,null;

2.1.3 删除表注释信息

  1. execute sp_dropextendedproperty 'MS_Description','user','dbo','table',
  2. '表名',null,null;

2.1.4 查询表注释信息

  1. select isnull(value,'') from sys.extended_properties ex_p where ex_p.minor_id=0
  2. and ex_p.major_id in (select id from sys.sysobjects a where a.name='表名')

3. 数据库表字段添加注释

3.1 表字段添加注释信息

3.1.1 添加字段注释信息

  1. EXEC sp_addextendedproperty 'MS_Description', '字段注释', 'user', dbo,
  2. 'table', 表名, 'column', 字段;

3.1.2 修改字段注释信息

  1. EXEC sp_updateextendedproperty 'MS_Description', '测试1112', 'user', dbo,
  2. 'table', 表名, 'column', 字段;

3.1.3 删除字段注释信息

  1. execute sp_dropextendedproperty 'MS_Description','user','dbo','table',
  2. '表名','column','字段';

3.1.4 查询字段注释信息

  1. SELECT
  2. A.name AS table_name,
  3. B.name AS column_name,
  4. C.value AS column_description
  5. FROM sys.tables A
  6. INNER JOIN sys.columns B ON B.object_id = A.object_id
  7. LEFT JOIN sys.extended_properties C ON C.major_id = B.object_id AND C.minor_id = B.column_id
  8. WHERE A.name = '表名'