获取SqlServer2005表结构(字段,主键,外键,递增,描述)
1.获取表的基本字段属性 --获取SqlServer中表结构
SELECT syscolumns.name,systypes.name,syscolumns.isnullable,
syscolumns.length
FROMsyscolumns, systypes
WHERE syscolumns.xusertype = systypes.xusertype
AND syscolumns.id=
object_id('你的表名')
运行效果
![[转载]SQLServer2005表结构查询语句 [转载]SQLServer2005表结构查询语句](http://www.cnblogs.com/images/cnblogs_com/eflylab/200711/2008-06-23_002758.jpg)
2.如果还想要获取字段的描述信息则
--获取SqlServer中表结构 主键,及描述
declare@table_name as varchar(max)
set @table_name ='你的表名'
selectsys.columns.name, sys.types.name,sys.columns.max_length, sys.columns.is_nullable,
(select
count(*) from sys.identity_columns where sys.identity_columns.object_id
= sys.columns.object_id
and sys.columns.column_id = sys.identity_columns.column_id) as is_identity ,
(select value fromsys.extended_properties where sys.extended_properties.major_id = sys.columns.object_id
and sys.extended_properties.minor_id = sys.columns.column_id) as description
fromsys.columns, sys.tables, sys.typeswhere sys.columns.object_id
= sys.tables.object_id
and sys.columns.system_type_id=sys.types.system_type_id and sys.tables.name=@table_name
order
by sys.columns.column_id
运行效果
![[转载]SQLServer2005表结构查询语句 [转载]SQLServer2005表结构查询语句](http://www.cnblogs.com/images/cnblogs_com/eflylab/200711/2008-06-23_003008.jpg)
3.单独查询表的递增字段
--单独查询表递增字段
select[name] from syscolumnswhere id=object_id(N'你的表名') and COLUMNPROPERTY(id,name,'IsIdentity')=1
运行效果
![[转载]SQLServer2005表结构查询语句 [转载]SQLServer2005表结构查询语句](https://i-blog.csdnimg.cn/blog_migrate/430365bd39570d143b32f31a98501c9e.jpeg)
4.获取表的主外键
--获取表主外键约束
execsp_helpconstraint '你的表名' ;
运行效果
![[转载]SQLServer2005表结构查询语句 [转载]SQLServer2005表结构查询语句](http://www.cnblogs.com/images/cnblogs_com/eflylab/200711/2008-06-23_003431.jpg)
SELECT syscolumns.name,systypes.name,syscolumns.isnullable,
syscolumns.length
FROMsyscolumns, systypes
WHERE syscolumns.xusertype = systypes.xusertype
AND syscolumns.id=
object_id('你的表名')
运行效果
![[转载]SQLServer2005表结构查询语句 [转载]SQLServer2005表结构查询语句](http://www.cnblogs.com/images/cnblogs_com/eflylab/200711/2008-06-23_002758.jpg)
2.如果还想要获取字段的描述信息则
--获取SqlServer中表结构 主键,及描述
declare@table_name as varchar(max)
set @table_name ='你的表名'
selectsys.columns.name, sys.types.name,sys.columns.max_length, sys.columns.is_nullable,
(select
count(*) from sys.identity_columns where sys.identity_columns.object_id
= sys.columns.object_id
and sys.columns.column_id = sys.identity_columns.column_id) as is_identity ,
(select value fromsys.extended_properties where sys.extended_properties.major_id = sys.columns.object_id
and sys.extended_properties.minor_id = sys.columns.column_id) as description
fromsys.columns, sys.tables, sys.typeswhere sys.columns.object_id
= sys.tables.object_id
and sys.columns.system_type_id=sys.types.system_type_id and sys.tables.name=@table_name
order
by sys.columns.column_id
运行效果
![[转载]SQLServer2005表结构查询语句 [转载]SQLServer2005表结构查询语句](http://www.cnblogs.com/images/cnblogs_com/eflylab/200711/2008-06-23_003008.jpg)
3.单独查询表的递增字段
--单独查询表递增字段
select[name] from syscolumnswhere id=object_id(N'你的表名') and COLUMNPROPERTY(id,name,'IsIdentity')=1
运行效果
![[转载]SQLServer2005表结构查询语句 [转载]SQLServer2005表结构查询语句](https://i-blog.csdnimg.cn/blog_migrate/430365bd39570d143b32f31a98501c9e.jpeg)
4.获取表的主外键
--获取表主外键约束
execsp_helpconstraint '你的表名' ;
运行效果
![[转载]SQLServer2005表结构查询语句 [转载]SQLServer2005表结构查询语句](http://www.cnblogs.com/images/cnblogs_com/eflylab/200711/2008-06-23_003431.jpg)