Monday, September 26, 2011

Preparing “Data Dictionary” using SQL Query

Data dictionary is the required while documenting any project. 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

Just include the column of the table definition (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).


Learn by diving in Programming Ocean...
Happy Programming!!!

1 comment:

  1. Below given details might be also useful
    --Finding all details of Primary Key constraint
    select * from sysobjectswhere xtype='PK'

    --Finding all details of Foreign Key constraint
    select * from sysobjectswhere xtype='F'

    --Finding all User-Defined objects (tables, etc)
    select * from sysobjectswhere xtype='U'

    --Finding all System objects
    select * from sysobjectswhere xtype='S'

    --Finding all user names
    select * from sysusers

    --Finding Column Names of Particular Table
    --Select Pubs Database
    select c.name from sysobjects o, syscolumns cwhere o.id = c.id ando.name = 'publishers'

    ReplyDelete