1.获取所有数据库名:
SELECT Name from Master..SysDatabases ORDER BY Name
2.获取所有表名:
SELECT Name from DatabaseName..SysObjects Where XType='U' ORDER BY Name
XType='U':表示所有用户表;
XType='S':表示所有系统表;
3.获取所有字段名:
SELECT Name from SysColumns WHERE id=Object_Id('TableName')
access :
如果表存在,如何得到这个表?
SELECT * from MSysObjects where name='ExamResultTime ' and type=1 and flags=0
//得到该数据库中所有的表名
SELECT name from MSysObjects where type=1 and flags=0
- 1、获取所有用户名:
-
Select
name FROM Sysusers wherestatus= '2'and islogin= '1' -
islogin='1'
:表示帐户 -
islogin='0'
:表示角色 -
status='2'
:表示用户帐户 -
status='0'
:表示糸统帐户 -
- 2、获取所有数据库名:
-
Select
Name FROM Master..SysDatabases orDERBY Name -
- 3、获取所有表名:
-
Select
Name FROM DatabaseName..SysObjects WhereXType= 'U'orDER BY Name -
XType='U'
:表示所有用户表; -
XType='S'
:表示所有系统表; -
- 4、获取所有字段名:
-
Select
Name FROM SysColumns Whereid=Object_Id( '表名'') -
- 5、获取数据库所有类型:
-
select
name from systypes -
- 6、获取主键字段:
-
Select
FROM SysColumns Whereid=Object_Id( '表名')and colid=( selecttop 1 fromkeyno sysindexkeys whereid=Object_Id( '表名')) -
- 7、获取表字段的基本信息:
-
程序代码 -
Select
-
字段名=rtrim(b. -
主键= WHEN h.id ISNOT NULL 'PK' ELSE '' END, -
字段类型=type_name(b.xusertype)+ WHEN b.colstat&1=1 THEN'[ID(' + CONVERT(varchar,ident_seed(a. name))+','+CONVERT(varchar,ident_incr(a.name))+')]'ELSE '' END, -
长度=b.length, -
允许空= b.isnullable WHEN0 THEN'N' ELSE 'Y' END, -
默认值= ''), -
字段说明= '') -
FROM
sysobjects a, syscolumns b -
LEFT
OUTER JOIN sysproperties ONc b.id AND= c.id b.colid = c.smallid -
LEFT
OUTER JOIN syscomments ONe b.cdefault = e.id -
LEFT
OUTER JOIN ( Selectg.id, FROMg.colid sysindexes Wheref, sysindexkeys g (f.id=g.id) AND(f.indid=g.indid)AND(f.indid>0)AND(f.indid<255)AND(f.status&2048)<>0)h ON(b.id=h.id) AND(b.colid=h.colid) -
Where
(a.id=b.id) AND(a.id=object_id('要查询的表')) -
orDER
BY b.colid -
-
程序代码 -
Select
-
表名= when a.colorder=1 thend. nameelse '' end, -
表说明= when a.colorder=1 thenisnull(f.value,'') else '' end, -
字段序号=a.colorder, -
字段名=a. -
标识= when COLUMNPROPERTY(a.id,a. name,'IsIdentity')=1then '√' else '' end, -
主键= when exists( Select1 FROMsysobjects wherextype= 'PK'and name in ( Selectname FROM sysindexes Whereindid in(Selectindid FROMsysindexkeys Whereid=a.id ANDcolid=a.colid))) then'√' else '' end, -
类型=b. -
字段长度=a.length, -
占用字节数=COLUMNPROPERTY(a.id,a. -
小数位数= -
允许空= when a.isnullable=1 then'√'else '' end, -
默认值= -
字段说明= -
FROM
syscolumns a -
left
join systypes onb a.xusertype=b.xusertype -
inner
join sysobjects ond (a.id=d.id) and(d.xtype='U')and(d.name<>'dtproperties') -
left
join syscomments one a.cdefault=e.id -
left
join sysproperties ong (a.id=g.id) and(a.colid=g.smallid) -
left
join sysproperties onf (d.id=f.id) and(f.smallid=0) -
--where
d.name='要查询的表' --如果只查询指定表,加上此条件 -
order
by a.id,a.colorder