在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 syscolumnsINNER 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.htmlSELECT 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 syscolumnsINNER 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 = @TableNameParorder by sysobjects.name
上述代码同样在Microsoft SQL Server 2012 (SP1)- 11.0.3128.0(X64)中经测试可以使用。
本文参考:显示 Sql Server 中所有表中的信息
更多:SQL中变量的定义
新闻热点
疑难解答