数据库名、数据表名、字段名、主键、标识列相关查询

时间:2021-04-08 13:45:21

使用于SQL SERVER 2008
----查询所有数据库名

1 SELECT Name FROM Master..SysDatabases ORDER BY Name

----查询某数据库中的所有表名

1 SELECT Name FROM 数据库名..SysObjects Where XType='U' ORDER BY Name

----查询数据表的字段信息

 1 select 
 2 a.name as FieldName,  -- 字段名
 3 a.isnullable,         -- 是否可为空
 4 b.Value as FieldDesc, -- 字段说明
 5 c.name as FieldType,  -- 数据类型
 6 COLUMNPROPERTY(a.id,a.name,'IsIdentity') as isidentity,  --是否标识列
 7 PK=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (
 8 SELECT name FROM sysindexes WHERE indid in(
 9 SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid
10 ))) then 'true' else 'false' end  --是否主键
11 from SysColumns a left join
12  sysproperties b on a.id=b.id and a.colid=b.smallid left join systypes c on a.xusertype=c.xusertype
13  where a.id=Object_Id('数据表名')