Skip to content

Instantly share code, notes, and snippets.

@jeremykdev
Last active August 29, 2015 13:55
Show Gist options
  • Save jeremykdev/8697777 to your computer and use it in GitHub Desktop.
Save jeremykdev/8697777 to your computer and use it in GitHub Desktop.
Query for table meta data for SQL server database. Will show description for columns created in SQL managment studio visual designer. These comments are stored as 'MS_Description'. To show other extended properties change the EP.name value in the LEFT JOIN clause.
SELECT
S.name AS SchemaName
, T.name AS TableName
, C.name AS ColumnName
, EP.value AS ColumnDescription
, Y.name AS DataType
, C.max_length AS MaximumLength
, C.is_nullable AS AllowsNulls
, C.precision AS Precision
, C.scale AS Scale
FROM sys.tables AS T
INNER JOIN sys.schemas AS S
ON ( T.schema_id = S.schema_id )
INNER JOIN sys.columns AS C
ON ( T.object_id = C.object_id )
INNER JOIN sys.types AS Y
ON ( Y.user_type_id = C.user_type_id )
LEFT OUTER JOIN sys.extended_properties AS EP
ON ( EP.class = 1 AND EP.major_id = C.object_id AND EP.minor_id = C.column_id AND EP.name = 'MS_Description' )
ORDER BY S.name, T.name, C.name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment