This query returns list of views and their columns in a database sorted by schema, view and column name with details.
SQL Query
select schema_name(v.schema_id) AS SCHEMA_NAME, v.name AS VIEW_NAME, col.name AS COLUMN_NAME, t.name AS DATA_TYPE, t.name + case when t.is_user_defined = 0 then isnull('(' + case when t.name in ('binary', 'char', 'nchar', 'varchar', 'nvarchar', 'varbinary') then case col.max_length when -1 then 'MAX' else case when t.name in ('nchar', 'nvarchar') then cast(col.max_length/2 as varchar(4)) else cast(col.max_length as varchar(4)) end end when t.name in ('datetime2', 'datetimeoffset', 'time') then cast(col.scale as varchar(4)) when t.name in ('decimal', 'numeric') then cast(col.precision as varchar(4)) + ', ' + cast(col.scale as varchar(4)) end + ')', '') else ':' + (select c_t.name + isnull('(' + case when c_t.name in ('binary', 'char', 'nchar', 'varchar', 'nvarchar', 'varbinary') then case c.max_length when -1 then 'MAX' else case when t.name in ('nchar', 'nvarchar') then cast(c.max_length/2 as varchar(4)) else cast(c.max_length as varchar(4)) end end when c_t.name in ('datetime2', 'datetimeoffset', 'time') then cast(c.scale as varchar(4)) when c_t.name in ('decimal', 'numeric') then cast(c.precision as varchar(4)) + ', ' + cast(c.scale as varchar(4)) end + ')', '') from sys.columns as c inner join sys.types as c_t on c.system_type_id = c_t.user_type_id where c.object_id = col.object_id and c.column_id = col.column_id and c.user_type_id = col.user_type_id ) end AS DATA_TYPE_EXT, case when col.is_nullable = 0 then 'N' else 'Y' end as ISNULLABLE, ep.value as COMMENTS from sys.views as v join sys.columns as col on v.object_id = col.object_id left join sys.types as t on col.user_type_id = t.user_type_id left join sys.extended_properties as ep on v.object_id = ep.major_id and col.column_id = ep.minor_id and ep.name = 'MS_Description' and ep.class_desc = 'OBJECT_OR_COLUMN' order by SCHEMA_NAME, VIEW_NAME, COLUMN_NAME;
Rows
One row represents one view column. All view columns will be included.
Columns
Column | Description |
SCHEMA_NAME | Schema name. |
VIEW_NAME | View name. |
COLUMN_NAME | View column name. |
DATA_TYPE | Data type. Example: – varchar or decimal. |
DATA_TYPE_EXT | Data type with information about scale/precision or string length. Example: – varchar(100) or decimal(8, 2). |
ISNULLABLE | Nullable flag. “Y” if column is nullable, “N” if column is not nullable. |
COMMENTS | Column comments. |
Output