There is often some basic information that you want to see regarding the tables and columns in your current database.
Select t.TABLE_NAME As [Table], c.COLUMN_NAME As [Column],c.DATA_TYPE As [DataType],c.CHARACTER_MAXIMUM_LENGTH As [Length],CaseWhen c.IS_NULLABLE = 'Yes'Then 1Else 0End As [Nullable],Left(k.CONSTRAINT_NAME, 2) As [KeyType],d.Description As [MS_Description]From INFORMATION_SCHEMA.TABLES tInner Join INFORMATION_SCHEMA.COLUMNS cOn t.TABLE_NAME = c.TABLE_NAMELeft Outer Join INFORMATION_SCHEMA.KEY_COLUMN_USAGE kOn t.TABLE_NAME = k.TABLE_NAMEAnd c.COLUMN_NAME = k.COLUMN_NAMELeft Outer Join (Select o.Name As [Table],c.name As [Column],ep.value As [Description]From sys.objects oInner Join sys.extended_properties epOn o.object_id = ep.major_idLEFT Join syscolumns cOn ep.minor_id = c.colidAnd ep.major_id = c.idWhere o.type = 'U') As dOn t.TABLE_NAME = d.[Table]And c.COLUMN_NAME = d.[Column]Where t.TABLE_TYPE = 'BASE TABLE'And t.TABLE_NAMENot In ('sysdiagrams')Order By t.TABLE_NAME, c.ORDINAL_POSITIONI hope this saves everyone else as much time as it saves me! It also includes the MS_Description schema property in case you document your databases in hopes that .NET will support a self-documenting database architecture some day.
No comments:
Post a Comment