获得SQL数据库信息的代码
程序员文章站
2023-12-11 17:55:40
复制代码 代码如下: select name from master..sysdatabases order by name --获得所有数据库 select name f...
复制代码 代码如下:
select name from master..sysdatabases order by name --获得所有数据库
select name from [数据库名]..sysobjects where xtype='u' order by name --获得数据库下的所有表
select name from syscolumns where id=object_id(n'表名') --获得表里的所有字段
--获取表字段名、类型、递增、描述
复制代码 代码如下:
select syscolumns.name as 字段,syscolumns.isnullable as 可否为空,systypes.name as sql类型,
isnull(sys.identity_columns.is_identity,0) as 递增,
isnull(sys.extended_properties.value,'') as 描述 from sysobjects
join syscolumns on sysobjects.id = syscolumns.id
join systypes on syscolumns.xusertype = systypes.xusertype
left join sys.identity_columns on sys.identity_columns.object_id = syscolumns.id
and sys.identity_columns.column_id = syscolumns.colid
left join sys.extended_properties on sys.extended_properties.major_id = syscolumns.id
and sys.extended_properties.minor_id = syscolumns.colid
where sysobjects.name = '表名'