获取数据库内所有的表和表内字段的信息

本文提供了SQL Server中查询所有表及视图的元数据信息的方法,包括表名、字段名、字段类型等详细属性,适用于数据库管理和应用开发。

获取所有的表:

SELECT OBJECT_NAME (id) FROM sysobjects WHERE xtype = 'U' AND OBJECTPROPERTY (id, 'IsMSShipped') = 0

 

获取表内字段的信息:

SELECT d.name AS c_table, a.colorder AS c_OrderID, a.name AS c_name, a.colstat AS c_IsIdentity, CASE WHEN EXISTS (SELECT 1 FROM sysindexes y, sysindexkeys z WHERE y.id = z.id AND y.indid = z.indid AND z.id = a.id AND z.colid = a.colid AND y.status & 2948 = 2048) THEN '1' ELSE '0' END AS c_IsPK, b.name AS c_type, a.length AS c_ByteNumber, a.prec AS c_Length, ISNULL(a.scale, 0) AS c_DecimalDigits, a.isnullable AS c_IsAllowNull, ISNULL(e.text, '') AS c_Default, ISNULL(g.[value], '') AS c_Description 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 sys.extended_properties g ON a.id = G.major_id AND a.colid = g.minor_id LEFT JOIN sys.extended_properties f ON d.id = f.major_id AND f.minor_id = 0 WHERE d.name <> 'sysdiagrams' ORDER BY a.id,a.colorder

 

 

获取表和视图内所有的字段信息

SELECT d.name AS c_table,  a.name AS c_name, a.colstat AS c_IsIdentity, CASE WHEN EXISTS (SELECT 1 FROM sysindexes y, sysindexkeys z WHERE y.id = z.id AND y.indid = z.indid AND z.id = a.id AND z.colid = a.colid AND y.status & 2948 = 2048) THEN '1' ELSE '0' END AS c_IsPK, b.name AS c_type, a.length AS c_ByteLength,   a.isnullable AS c_IsAllowNull 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' or d.xtype='V') 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 LEFT JOIN sys.extended_properties f ON d.id = f.major_id AND f.minor_id = 0 WHERE d.name <> 'sysdiagrams' ORDER BY a.id,a.colorder

 

转载于:https://www.cnblogs.com/feihusurfer/p/6210271.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值