[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2013-11-05
2
Medium Priority
?
648 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 66

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 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

649 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