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: Tuesday, June 17, 2014 7:26 AM
Points: 16, Visits: 480
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


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Monday, July 21, 2014 6:24 AM
Points: 3,537, Visits: 2,647
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: Monday, July 21, 2014 8:48 AM
Points: 1, Visits: 254
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


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 10:07 PM
Points: 3,617, Visits: 5,236
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.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
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?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1360565
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse