SQL经验分享(二)取得数据库中所有的表名、字段名以及字段属于哪个表
时间:2011-05-29 来源:独孤伤
2.1取得数据库中所有表名
样例数据:
select t.name '表名' from sysobjects t where OBJECTPROPERTY(t.id, N'IsUserTable') = 1
或者用select name from sysobjects where type='U'
执行结果:
语句:
select distinct c.name '字段名' from sysobjects t, syscolumns c
where t.id = c.id and OBJECTPROPERTY(t.id, N'IsUserTable') = 1
执行结果:
2.3查所有表和字段
语句:select t.name '表名' ,c.name '字段名' from sysobjects t, syscolumns c
where t.id = c.id and OBJECTPROPERTY(t.id, N'IsUserTable') = 1 group by t.name,c.name
执行结果:
2.4查某个表的所有字段
Table_1原始数据:
语句:
select t.name,c.name '字段名' from sysobjects t, syscolumns c
where t.id = c.id and OBJECTPROPERTY(t.id, N'IsUserTable') = 1
and t.name='Table_1'
执行结果:
2.5查字段属于哪个表(即找含有相同字段的表)
查询姓名列在哪些表中有
语句:
select distinct t.name from sysobjects t, syscolumns c
where t.id = c.id and OBJECTPROPERTY(t.id, N'IsUserTable') = 1
and c.name in ('姓名')
执行结果: