SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to find duplicate default constraints in sql server


how to find duplicate default constraints in sql server

Author
Message
achtro
achtro
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
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
sqlnaive
sqlnaive
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4291 Visits: 2774
Check this query:

SELECT CONSTRAINT_NAME, COUNT(*) FROM INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE
GROUP BY CONSTRAINT_NAME
HAVING COUNT(*) > 1
Not A DBA
Not A DBA
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 351
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.
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)

Group: General Forum Members
Points: 7325 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search