谁知道几种查询表结构的方法

来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/23 15:43:29
谁知道几种查询表结构的方法谁知道几种查询表结构的方法谁知道几种查询表结构的方法方法一:使用存储结构来执行,查询某一个表的结构createprocgetTableStruct@tabNamevarcha

谁知道几种查询表结构的方法
谁知道几种查询表结构的方法

谁知道几种查询表结构的方法
方法一:使用存储结构来执行,查询某一个表的结构create proc getTableStruct @tabName varchar(20) as SELECT 表名 = case when a.colorder=1 then d.name else '' end,表说明 = case when a.colorder=1 then isnull(f.value,'') else '' end,字段序号 = a.colorder,字段名 = a.name,标识 = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,主键 = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in ( SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '√' else '' end,类型 = b.name,占用字节数 = a.length,长度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'),小数位数 = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),允许空 = case when a.isnullable=1 then '√'else '' end,默认值 = isnull(e.text,''),字段说明 = isnull(g.[value],'') FROM syscolumns a left join systypes b on a.xusertype=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 sysproperties g on a.id=g.id and a.colid=g.smallid left join sysproperties f on d.id=f.id and f.smallid=0 where d.name in (@tabName) --如果只查询指定表,加上此条件 order by a.id,a.colorder GO 方法二:使用存储过程来执行,得到的是这个数据库中所有表及其结构 CREATE proc sp_tableStruct as ----- 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 长度,isnull ( COLUMNPROPERTY ( a.id,a.name ,'Scale'),0) as 小数位数,(case when a.isnullable = 1 then '√' else '' end ) 允许空,isnull ( e.text,'') 默认值,isnull (g.[value],'' ) 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 sysproperties g on a.id = g.id and a.colid = g.smallid order by a.id ,a.colorder GO 方法三:使用查询来查找所有表及表的结构 SELECT c.colid AS 序号,o.name AS 表名,c.name AS 列名,t.name AS 类型,c.length AS 长度,c.isnullable AS 允许空,CAST(m.[value] AS Varchar(100)) AS 说明 FROM dbo.syscolumns c INNER JOIN dbo.sysobjects o ON o.id = c.id AND objectproperty(o.id,N'IsUserTable') = 1 AND o.name 'dtproperties' INNER JOIN dbo.systypes t ON t.xusertype = c.xusertype LEFT OUTER JOIN dbo.sysproperties m ON m.id = o.id AND m.smallid = c.colorder ORDER BY o.name,c.colid