All access to the database tables and field names, type, length of the SQL 2008 SQL Server

For some database because we do not have permission or other reasons can not view its database table structure, then we will have to think of ways to do it, if we can access the application, then we can use as shown below

sql script to get all tables in the database structure, the code is as follows:

 

use AdventureWorks2008
go

 

SELECT
(case when a.colorder=1 then d.name else '' end) 表名,
a.colorder 字段序号,
a.name 字段名,
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) 标识,
(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) 主键,
b.name 类型,
a.length 占用字节数,
COLUMNPROPERTY (a.id, a.name, 'PRECISION ') as length,
ISNULL (the COLUMNPROPERTY (a.id, a.name, 'Scale'), 0) AS decimal places,
(Case When the then a.isnullable. 1 = '√'else' 'end) to allow air,
ISNULL (e.text,' ') default,
ISNULL (G. [value],' ') field specifies the AS   

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 sys.extended_properties g on a.id=g.major_id AND a.colid = g.minor_id 
--where d.name in ('Contact','StockBmps','AddressType')---查询具体的表,注释掉后就是查询整个数据库了
order by a.id,a.colorder

If the version of the database is not SQL Server2008 it, such as SQL Server2000 is it?

Then you need to sys.extended_properties with sysproperties instead.

Because sysproperties the system tables, but was not able to find the 2008 version, in the online documentation to be found, and later found that the system table has been replaced by the system tables sys.extended_properties in version 2005.

 

View extended information table T:

select object_id from sys.sysobjects where name = 'T';

select * from sys.extended_properties where major_id = object_id;

Extended information item has a name is MS_Description, this option allows you to view information notes

select * from sys.extended_properties where major_id = object_id where name = 'MS_Description';

 

 

 

Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2012/07/25/2671014.html

Guess you like

Origin blog.csdn.net/weixin_33857679/article/details/93052951