how to find duplicate default constraints in sql server

  • 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

  • Check this query:

    SELECT CONSTRAINT_NAME, COUNT(*) FROM INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE

    GROUP BY CONSTRAINT_NAME

    HAVING COUNT(*) > 1

  • 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.

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply