获得SQL数据库信息的代码

编程学习 2021-07-05 13:42www.dzhlxh.cn编程入门
获取表字段名、类型、递增、描述
代码如下:

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 = '表名'

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板