Solved

SQL Server: need a list of tables having DEFAULT constraint.

Posted on 2013-11-05
2
627 Views
Last Modified: 2013-11-19
Hi, I need a list of tables that have DEFAULT constraint on at least one of the column. Would be nice to be able to filter by column datatype and/or name.
Thanks.
0
Comment
Question by:quasar_ee
2 Comments
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
Copy-paste the below T-SQL into your SSMS, execute it to verify it works, then delete the WHERE clause
CREATE TABLE boo (foo varchar(100) DEFAULT 'banana')

SELECT t.name, c.name, dc.definition
FROM sys.tables t
	JOIN sys.columns c ON t.object_id = c.object_id
	JOIN sys.default_constraints dc ON c.default_object_id = dc.object_id
WHERE c.name = 'foo'

Open in new window

0
 
LVL 3

Accepted Solution

by:
mikeyd234 earned 500 total points
Comment Utility
Try this,  will output all default_constraints columns with their data types and the parent table name for each.

SELECT sys.default_constraints.name, 
       sys.all_columns.name AS column_name, 
       ty.name              AS column_type, 
       sys.tables.name      AS table_name 
FROM   sys.all_columns 
       INNER JOIN sys.tables 
               ON sys.all_columns.object_id = sys.tables.object_id 
       INNER JOIN sys.schemas 
               ON sys.tables.schema_id = sys.schemas.schema_id 
       INNER JOIN sys.default_constraints 
               ON sys.all_columns.default_object_id = 
                  sys.default_constraints.object_id 
       LEFT OUTER JOIN sys.types AS ty 
                    ON sys.all_columns.system_type_id = ty.system_type_id 
WHERE  ( sys.schemas.name = 'dbo' )
-- AND all_columns.name = 'columnname'  -- filter by column name
-- AND ty.name  = 'nvarchar'                   -- filter by data type
 

Open in new window

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now