SQL 获取一些实用信息的方法
2010-12-13 15:04
405 查看
1。获取数据库列表
针对SQL2005,对于所有的数据库,都存在系统表“sysdatabases”中,获取起来比较简单
select * from sysdatabases where name not in ('master','model','msdb','tempdb')
where后面的语句是为了不获取SQL的系统数据库,大家可以不加条件,就能获取所有数据库了。
2。获取指定数据库中的所有数据表/视图的列表
针对SQL2005,所有表/视图,都存储在“sysobjects”系统表中,而所有表的字段信息,则主要存储在“syscolumns”系统表中,另外,还有一个重要的系统表“systypes”主要存储数据库中系统定义和用户自定义的数据类型。
获取所有表的名字和每个表的字段数目:
string
SqlStr_Tbl = @"select sysobjects.name ,count(*) as ColsCount from
syscolumns,sysobjects,systypes where syscolumns.id=sysobjects.id and
syscolumns.xtype=systypes.xtype and sysobjects.type='U' and
systypes.name <>'_default_' and systypes.name<>'sysname'
group by sysobjects.name order by sysobjects.name";
获取所有视图的名字和每个视图的字段数目:
string
SqlStr_Vw = @"select sysobjects.name ,count(*) as ColsCount from
syscolumns,sysobjects,systypes where syscolumns.id=sysobjects.id and
syscolumns.xtype=systypes.xtype and sysobjects.type='V' and
systypes.name <>'_default_' and systypes.name<>'sysname'
group by sysobjects.name order by sysobjects.name";
3。获取指定表的所有字段属性
下面的语句中,只要代入不同的表/视图名,就可以获取该表的所有字段名字、字段长度、字段类型、字段说明、字段是否允许为空、是否主键、是否自增长字段等。
string
SqlStr = @"select syscolumns.name as ColName ,systypes.name as
ColTypeName , syscolumns.length, sys.extended_properties.value as Mark
,syscolumns.isnullable as AllowNull ,IsPK = Case when exists ( select 1
from sysobjects inner join sysindexes on sysindexes.name =
sysobjects.name inner join sysindexkeys on sysindexes.id =
sysindexkeys.id and sysindexes.indid = sysindexkeys.indid where
xtype='PK' and parent_obj = syscolumns.id and sysindexkeys.colid =
syscolumns.colid ) then 1 else 0 end ,IsIdentity = Case
syscolumns.status when 128 then 1 else 0 end from syscolumns inner join
systypes on ( syscolumns.xtype = systypes.xtype and systypes.name
<>'_default_' and systypes.name<>'sysname' ) left outer join
sys.extended_properties on (
sys.extended_properties.major_id=syscolumns.id and
minor_id=syscolumns.colid ) where syscolumns.id = (select id from
sysobjects where name='" + TableName + @"') order by syscolumns.colid ";
针对SQL2005,对于所有的数据库,都存在系统表“sysdatabases”中,获取起来比较简单
select * from sysdatabases where name not in ('master','model','msdb','tempdb')
where后面的语句是为了不获取SQL的系统数据库,大家可以不加条件,就能获取所有数据库了。
2。获取指定数据库中的所有数据表/视图的列表
针对SQL2005,所有表/视图,都存储在“sysobjects”系统表中,而所有表的字段信息,则主要存储在“syscolumns”系统表中,另外,还有一个重要的系统表“systypes”主要存储数据库中系统定义和用户自定义的数据类型。
获取所有表的名字和每个表的字段数目:
string
SqlStr_Tbl = @"select sysobjects.name ,count(*) as ColsCount from
syscolumns,sysobjects,systypes where syscolumns.id=sysobjects.id and
syscolumns.xtype=systypes.xtype and sysobjects.type='U' and
systypes.name <>'_default_' and systypes.name<>'sysname'
group by sysobjects.name order by sysobjects.name";
获取所有视图的名字和每个视图的字段数目:
string
SqlStr_Vw = @"select sysobjects.name ,count(*) as ColsCount from
syscolumns,sysobjects,systypes where syscolumns.id=sysobjects.id and
syscolumns.xtype=systypes.xtype and sysobjects.type='V' and
systypes.name <>'_default_' and systypes.name<>'sysname'
group by sysobjects.name order by sysobjects.name";
3。获取指定表的所有字段属性
下面的语句中,只要代入不同的表/视图名,就可以获取该表的所有字段名字、字段长度、字段类型、字段说明、字段是否允许为空、是否主键、是否自增长字段等。
string
SqlStr = @"select syscolumns.name as ColName ,systypes.name as
ColTypeName , syscolumns.length, sys.extended_properties.value as Mark
,syscolumns.isnullable as AllowNull ,IsPK = Case when exists ( select 1
from sysobjects inner join sysindexes on sysindexes.name =
sysobjects.name inner join sysindexkeys on sysindexes.id =
sysindexkeys.id and sysindexes.indid = sysindexkeys.indid where
xtype='PK' and parent_obj = syscolumns.id and sysindexkeys.colid =
syscolumns.colid ) then 1 else 0 end ,IsIdentity = Case
syscolumns.status when 128 then 1 else 0 end from syscolumns inner join
systypes on ( syscolumns.xtype = systypes.xtype and systypes.name
<>'_default_' and systypes.name<>'sysname' ) left outer join
sys.extended_properties on (
sys.extended_properties.major_id=syscolumns.id and
minor_id=syscolumns.colid ) where syscolumns.id = (select id from
sysobjects where name='" + TableName + @"') order by syscolumns.colid ";
相关文章推荐
- rails中获取url信息的一些方法
- IOS中一些常用的获取设备信息的方法
- reuqest获取服务器一些信息方法集合 java
- java获取服务器一些信息的方法
- Android Battery一些信息获取方法
- rails中获取url信息的一些方法
- sql 中获取数据库名、表名、存储过程等信息的方法
- C#获取电脑信息的一些方法
- VC下获取远程主机共享信息的一些方法
- java获取服务器一些信息的方法(服务器地址/相对路径/端口/项目名字)
- 关于mysql中实用的修改表信息的一些sql
- android 获取sim一些信息方法如下:
- rails中获取url信息的一些方法
- java获取服务器一些信息的方法
- Linux C 获取硬件信息的一些方法
- 一些获取AD中用户信息的powershell名, 非常实用。
- java获取服务器一些信息的方法
- 获取微信用户信息的一些常用方法
- rails中获取url信息的一些方法
- java获取服务器一些信息的方法