Dropping a constraint when the name can vary from database to database

Submitted by Jochus on Tue, 14/04/2015 - 20:14 | Posted in: Database

DECLARE @col_name VARCHAR(255)
DECLARE @command  VARCHAR(1000)
SET @column_name = '$YOUR_COLUMN_NAME'
SELECT @command = 'ALTER TABLE ' + @TABLE_NAME + ' DROP CONSTRAINT ' + d.name FROM sys.tables t JOIN sys.default_constraints d ON d.parent_object_id = t.object_id JOIN sys.columns c ON c.object_id = t.object_id AND c.column_id = d.parent_column_id WHERE t.name = @TABLE_NAME AND c.name = @column_name
EXECUTE (@command)

Add new comment

The content of this field is kept private and will not be shown publicly.


  • Lines and paragraphs break automatically.
  • You can caption images (data-caption="Text"), but also videos, blockquotes, and so on.
  • Web page addresses and email addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, <cpp>, <css>, <html5>, <java>, <javascript>, <php>, <sql>, <xml>. The supported tag styles are: <foo>, [foo].
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.