Finding Table Columns With Default Values

Posted: February 14, 2011 in SQL Server 2005

The following is a quick script to indentify all columns that have a default value in the database.

SELECT   t.name       table_name, 
         c.name       column_name, 
         d.definition default_value, 
         d.name       constraint_name
FROM     SYS.TABLES                         t 
         INNER JOIN SYS.SYSCOLUMNS          c  ON t.object_id = c.id 
         INNER JOIN SYS.DEFAULT_CONSTRAINTS d  ON t.object_id = d.parent_object_id 
                                              AND c.colid     = d.parent_column_id
ORDER BY t.name, 
         c.colid
GO
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s