获取数据库的 表名 和 字段名
http://topic.csdn.net/t/20060422/01/4705164.html
如何获取数据库的 表名 和 字段名 ?楼主dy314()2006-04-22 01:05:47 在 MS-SQL Server / 基础类 提问
知道一个数据库,
如何得知里面有多少个表,每个表的名称是什么?
每个表的字段名是什么,类型是什么?
如何编程获取?
6 楼ghtyan()回复于 2006-04-22 09:33:45 得分 5
获取数据库中用户表
select id,name from sysobjects where xtype='u'
获取某个表中的字段名和类型
select A.name,B.name as Type from syscolumns A
join systypes B on A.xtype=B.xusertype
where id = object_id('表名')
13 楼btlyeo(春天)回复于 2006-04-25 11:05:26 得分 20---获取全部表名,字段名和所有相关信息
SELECT b.name '表名',a.colorder '序号',a.name '字段名',c.name '类型',a.length '长度',a.isnullable '空值',
a.cdefault '默认值' FROM syscolumns a,sysobjects b,systypes c WHERE a.id=b.id and b.xtype='u'
and a.xtype=c.xtype order by b.name,a.colorder
------取表名
SELECT distinct(b.name) '表名' FROM syscolumns a,sysobjects b,systypes c WHERE a.id=b.id and b.xtype='u'
and a.xtype=c.xtype order by b.name
14 楼tntzbzc(华裔大魔王—抗日要从娃娃抓起)回复于 2006-04-25 11:08:58 得分 23还有个更强的语句,是邹建写的
SELECT
(case when a.colorder=1 then d.name else '' end) N'表名',
a.colorder N'字段序号',
a.name N'字段名',
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) N'标识',
(case when (SELECT count(*)
FROM sysobjects
WHERE (name in
(SELECT name
FROM sysindexes
WHERE (id = a.id) AND (indid in
(SELECT indid
FROM sysindexkeys
WHERE (id = a.id) AND (colid in
(SELECT colid
FROM syscolumns
WHERE (id = a.id) AND (name = a.name))))))) AND
(xtype = 'PK'))>0 then '√' else '' end) N'主键',
b.name N'类型',
a.length N'占用字节数',
COLUMNPROPERTY(a.id,a.name,'PRECISION') as N'长度',
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as N'小数位数',
(case when a.isnullable=1 then '√'else '' end) N'允许空',
isnull(e.text,'') N'默认值',
isnull(g.[value],'') AS N'字段说明'
--into ##tx
FROM syscolumns a left join systypes b
on a.xtype=b.xusertype
inner join sysobjects d
on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e
on a.cdefault=e.id
left join sysproperties g
on a.id=g.id AND a.colid = g.smallid
order by object_name(a.id),a.colorder