日期:2014-05-16  浏览次数:20469 次

SQL经验分享(二)取得数据库中所有的表名、字段名以及字段属于哪个表

2.1取得数据库中所有表名

样例数据:



?

语句:

select t.name '表名' from sysobjects t where OBJECTPROPERTY(t.id, N'IsUserTable') = 1

或者用select name from sysobjects where type='U'

执行结果:



?

?

2.2 取得所有表中的所有字段名

语句:

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 ('姓名')

执行结果:



?