SQL Tips #21 - Listing All Table Columns and Their Default Values


Sunday, 21 January 2018

This query lists every table column that has a default value defined, along with the default itself.

(optional so.name='xxx' to filter to a specific table)

The Query

SELECT so.name AS table_name, sc.name AS column_name, sm.text AS default_value 
FROM sys.sysobjects so JOIN sys.syscolumns sc ON sc.id = so.id 
LEFT JOIN sys.syscomments sm ON sm.id = sc.cdefault 
WHERE so.xtype = 'U'AND sm.text IS NOT NULL --AND so.name = @yourtable 
ORDER BY so.[name], sc.colid

Disclaimer

These queries are provided as a guide, and are by no means perfect. I didn't write all of these - many are collated from MSDN documentation and community sources over the years.

Tags

SQL, Programming
Share with: 

Useful SQL tips - how to list all table columns and their default values


Support this Site

Buy me a coffee

Developer Courses

Pluralsight - Hardcore Developer and IT Training
Skype Web SDK: Getting Started
Skype Web SDK: Audio & Video

Popular Articles

What is Kutamo?
Kilimanjaro Climb
Kilimanjaro 2015
Kilimanjaro 2013
Australian Postcodes
New Zealand Postcodes
Worldwide City Database

Favourite Links

Kilimanjaro Climbs
Kutamo Studios
Kutamo Meetings
Litzi
ErrLog.IO
Kutamo

Tags / Keywords

SQL, Programming