Home Forums SQL Server 2008 T-SQL (SS2K8) how to find duplicate default constraints in sql server RE: how to find duplicate default constraints in sql server

  • 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