Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

how to find duplicate default constraints in sql server Expand / Collapse
Author
Message
Posted Monday, September 17, 2012 7:37 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, May 16, 2013 7:01 AM
Points: 16, Visits: 296
how to find duplicate default constraints in a table ,example DF_abc1 is a default constraint FOR column Effective date and again DF_efg1 is also a default constraint FOR same column Effective date,Now i have a scenario where i want to find the all duplicate default constraints on all the tables in a database, does anybody have any script for this .

thanks
Post #1360200
Posted Monday, September 17, 2012 8:33 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 4:30 AM
Points: 2,657, Visits: 1,662
Check this query:

SELECT CONSTRAINT_NAME, COUNT(*) FROM INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE
GROUP BY CONSTRAINT_NAME
HAVING COUNT(*) > 1
Post #1360243
Posted Monday, September 17, 2012 3:14 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, May 15, 2013 4:41 PM
Points: 1, Visits: 150
achtro (9/17/2012)
how to find duplicate default constraints in a table ,example DF_abc1 is a default constraint FOR column Effective date and again DF_efg1 is also a default constraint FOR same column Effective date,Now i have a scenario where i want to find the all duplicate default constraints on all the tables in a database, does anybody have any script for this .

thanks


Perhaps your question is worded strangely, but you can't have duplicate default constraints for a column in a table. So you wouldn't need to look for duplicates.

If you're looking for default constraints with the same name across a number of tables then
sqlnaive's answer is what you want.
Post #1360473
Posted Monday, September 17, 2012 11:38 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 3:20 AM
Points: 2,341, Visits: 3,175
I have to agree with NOT A DBA in that your question is worded strangely and his answer is basically correct.

I suspect you may be looking for something else, namely columns that appear like-named in different tables and have the same default values, but applied through a different DEFAULT constraint.

This query probably will help to get you started, if this is what you seek:

SELECT TABLE_NAME=b.name, COLUMN_NAME=a.name, CONSTRAINT_NAME=c.name, d.COLUMN_DEFAULT 
FROM sys.all_columns a
INNER JOIN sys.tables b
ON a.object_id = b.object_id
INNER JOIN sys.default_constraints c
ON a.default_object_id = c.object_id
INNER JOIN information_schema.columns d
ON b.name = d.TABLE_NAME AND a.name = d.COLUMN_NAME
WHERE c.type_desc = 'DEFAULT_CONSTRAINT'


It lists all columns from all tables that have a DEFAULT CONSTRAINT, along with the DEFAULT CONSTRAINT name and the default value it assigns.



No loops! No CURSORs! No RBAR! Hoo-uh!

INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1360565
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse