Solved

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

Posted on 2013-11-05
2
641 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39625242
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
ID: 39625244
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
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…

730 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