SQL Experience Sharing (ii) obtain all database table names, field names and field belongs to which table

2.1 Get all the table names in the database

Sample data:

SQL Experience Sharing (ii) obtain all database table names, field names and field belongs to which table
Statement:

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

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

Results of the:

SQL Experience Sharing (ii) obtain all database table names, field names and field belongs to which table

2.2 Get all field names in all tables

Statement:

select distinct c.name '字段名'  from sysobjects t, syscolumns c
where t.id = c.id   and  OBJECTPROPERTY(t.id, N'IsUserTable') = 1

Results of the:

SQL Experience Sharing (ii) obtain all database table names, field names and field belongs to which table
Note: Fields more, slightly behind

 

2.3 to view all the tables and fields

语句: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

Results of the:

SQL Experience Sharing (ii) obtain all database table names, field names and field belongs to which table
Note: Fields more, slightly behind

 

2.4 check all the fields to a table

Table_1 raw data:

SQL Experience Sharing (ii) obtain all database table names, field names and field belongs to which table
语句:
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'

Results of the:

SQL Experience Sharing (ii) obtain all database table names, field names and field belongs to which table


 

Table 2.5 check field belongs (i.e., containing the same table to find the field)

Query names are listed in the table which have

Statement:

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

Results of the:

SQL Experience Sharing (ii) obtain all database table names, field names and field belongs to which table

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2011/05/29/2062040.html

Guess you like

Origin blog.csdn.net/weixin_33709609/article/details/93496117