显示 Sql Server 中所有表或表中行的信息
在MSSQL中显示某个数据库中所有表或视图的信息:
(以下语句为获取所有表信息,将绿色字"U"替换为"V"则获取所有视图信息。)
SELECT sysobjects.name AS TABLE_NAME, syscolumns.Id, syscolumns.name AS COLUMN_NAME, systypes.name AS DATA_TYPE, syscolumns.length as COLUMN_MAX_LENGTH, sys.extended_properties.[value] AS COLUMN_DESCRIPTION, syscomments.text as COLUMN_DEFAULT, syscolumns.isnullable as IS_NULLABLE FROM syscolumns INNER JOIN systypes ON syscolumns.xtype = systypes.xtype LEFT JOIN sysobjects ON syscolumns.id = sysobjects.id LEFT OUTER JOIN sys.extended_properties ON (sys.extended_properties.minor_id = syscolumns.colid AND sys.extended_properties.major_id = syscolumns.id) LEFT OUTER JOIN syscomments ON syscolumns.cdefault = syscomments.id WHERE syscolumns.id IN (SELECT id FROM SYSOBJECTS WHERE xtype = 'U') AND (systypes.name <> 'sysname') order by sysobjects.name
上述代码在 Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 中经测试可以使用。
对于 Sql Server 2000 版本或 Sql Server 2005版本请参考:显示 Sql Server 中所有表中的信息
可用上述代码筛选获取某个表中的所有行信息:
declare @TableNamePar varchar(50) set @TableNamePar = 'TableOne'
--本文地址:http://www.cnblogs.com/Interkey/p/3770763.html SELECT sysobjects.name AS TABLE_NAME, syscolumns.Id, syscolumns.name AS COLUMN_NAME, systypes.name AS DATA_TYPE, syscolumns.length as COLUMN_MAX_LENGTH, sys.extended_properties.[value] AS COLUMN_DESCRIPTION, syscomments.text as COLUMN_DEFAULT, syscolumns.isnullable as IS_NULLABLE FROM syscolumns INNER JOIN systypes ON syscolumns.xtype = systypes.xtype LEFT JOIN sysobjects ON syscolumns.id = sysobjects.id LEFT OUTER JOIN sys.extended_properties ON (sys.extended_properties.minor_id = syscolumns.colid AND sys.extended_properties.major_id = syscolumns.id) LEFT OUTER JOIN syscomments ON syscolumns.cdefault = syscomments.id WHERE syscolumns.id IN (SELECT id FROM SYSOBJECTS WHERE xtype = 'U') AND (systypes.name <> 'sysname') AND sysobjects.name = @TableNamePar order by sysobjects.name
上述代码同样在 Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 中经测试可以使用。
更多:SQL中变量的定义
作者:Cosmic_Spy
出处:http://www.cnblogs.com/Interkey/
有兴趣可以加群【.NET破解|反编译】请注明:博客园,谢谢~
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则肯定是你抄我的。
posted on 2014-06-05 17:28 Cosmic_Spy 阅读(1186) 评论(2) 编辑 收藏 举报