I answered a forum question the other day about how to list all the columns in a SQL Server database. That’s straightforward enough, but they also asked for all the extended properties for the column.
In case you need to do this, here’s some code:
SELECT s.[name] AS SchemaName,
t.[name] AS TableName,
c.[name] AS ColumnName,
c.is_nullable AS IsNullable,
typ.[name] AS DataTypeName,
c.max_length AS MaximumLength,
c.[precision] AS [Precision],
c.scale AS Scale,
ep.ExtendedPropertyName,
ep.ExtendedPropertyValue
FROM sys.columns AS c
INNER JOIN sys.tables AS t
ON t.object_id = c.object_id
INNER JOIN sys.schemas AS s
ON s.schema_id = t.schema_id
INNER JOIN sys.types AS typ
ON typ.system_type_id = c.system_type_id
AND typ.user_type_id = c.user_type_id
OUTER APPLY
(
SELECT ep.[name] AS ExtendedPropertyName,
ep.[value] AS ExtendedPropertyValue
FROM sys.extended_properties AS ep
WHERE ep.major_id = c.object_id
AND ep.minor_id = c.column_id
) AS ep
WHERE t.is_ms_shipped = 0
AND t.[name] <> N'sysdiagrams'
ORDER BY SchemaName, TableName, ColumnName, ExtendedPropertyName;
How it works
I start with the sys.columns view and join it to sys.tables and sys.schemas, to get the schema and table name. The other reason is to make sure it’s not a Microsoft-supplied table. I also wanted to exclude the sysdiagrams table that is created when you first create a database diagram using SQL Server Management Studio. (Curiously, that one’s not flagged as a Microsoft-supplied table).
2020-05-21