65.9K
CodeProject is changing. Read more.
Home

Preparing “Data Dictionary” using SQL Query

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3 votes)

Oct 7, 2011

CPOL
viewsIcon

17100

Introduction

Documentation plays an important role whereas "Data Dictionary" gives the idea about data stored using application.

Solution

So here is the query for preparing the data dictionary of any database.
SELECT [Table Name] = OBJECT_NAME(c.object_id)
,[Column Name] = c.name
,[Data Type] = t.name
,[Size] = c.max_length
,[Description] = isnull(ex.value,'')
FROM sys.columns c
LEFT OUTER JOIN sys.extended_properties ex ON (ex.major_id = c.object_id AND ex.minor_id = c.column_id AND ex.name = 'MS_Description')
INNER JOIN sys.types t ON c.system_type_id = t.system_type_id
WHERE OBJECTPROPERTY(c.object_id, 'IsMsShipped')=0
AND OBJECT_NAME(c.object_id) in (select name from sys.tables where type= 'U')
ORDER BY OBJECT_NAME(c.object_id), c.column_id

Enhancement

You can add/remove columns which you want to include from the definition of the table (if any other required - i.e. if you want to display whether the column is null/not null, just include the field from sys.objects table and so on).