查询某个表的字段名,字段类型,长度及字段说明,查询主键信息,查询自增列信息的存储过程

2016-12-15 11:24:32来源:CSDN作者:sxf359人点击

create PROCEDURE [dbo].[ziduan]
/* 
function:查询某个表的字段名,字段类型,长度及字段说明,并asc排序;查询主键信息;查询自增列信息 
*/  
(
@biaoming nvarchar(50)
 
)
 
AS
SET NOCOUNT ON 
 
 
   --获取字段名,长度,缺省值,字段说明sql2000可用
   --SELECT  [name] =   a.name, [type_name] = b.name,  length = a.prec,[default]=m.text,[description] = isnull(g.[value], ' ')  FROM   syscolumns   a left   join  systypes   b   on   a.xusertype=b.xusertype left   join    sysproperties   g   on   a.id=g.id   and   a.colid=g.smallid left join syscomments m on a.cdefault=m.id   where   a.id=object_id( @biaoming)   order by [name] asc


--sql2005以上可用
 SELECT  [name] =   a.name, [type_name] = b.name,  length = a.prec,[default]=m.text,[description] = isnull(g.[value], ' ')  FROM   syscolumns   a left   join  systypes   b   on   a.xusertype=b.xusertype left   join   sys.extended_properties   g   on   a.id=g.major_id   and   a.colid=g.minor_id left join syscomments m on a.cdefault=m.id   where   a.id=object_id( @biaoming)   order by [name] asc

--获取表的主键值
exec sp_pkeys @biaoming


--获取该表自增列的相关信息
Select so.name Table_name, --表名字
sc.name Identity_Column_name, --自增字段名字
ident_current(so.name) curr_value, --自增字段当前值
ident_incr(so.name) incr_value, --自增字段增长值
ident_seed(so.name) seed_value --自增字段种子值
from sysobjects so 
Inner Join syscolumns sc
on so.id = sc.id
and columnproperty(sc.id, sc.name, 'IsIdentity') = 1
Where upper(so.name) = upper(@biaoming)




SELECT  索引名称 = a.name ,
        表名 = c.name ,
        索引字段名 = d.name ,
        索引字段位置 = d.colid
FROM    sysindexes a
        JOIN sysindexkeys b ON a.id = b.id
                               AND a.indid = b.indid
        JOIN sysobjects c ON b.id = c.id
        JOIN syscolumns d ON b.id = d.id
                             AND b.colid = d.colid
WHERE   a.indid NOT IN ( 0, 255 )  
-- and   c.xtype='U'   and   c.status>0 --查所有用户表  
        AND c.name = @biaoming --查指定表  
ORDER BY c.name ,
        a.name ,
        d.name


set nocount off

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台