Dropping unnamed constraints
For dropping an individual unnamed default constrain on a column use the following code:
DECLARE @ConstraintName VARCHAR(256) SET @ConstraintName = ( SELECT obj.name FROM sys.columns col LEFT OUTER JOIN sys.objects obj ON obj.object_id = col.default_object_id AND obj.type = 'F' WHERE col.object_id = OBJECT_ID('TableName') AND obj.name IS NOT NULL AND col.name = 'ColunmName' ) IF(@ConstraintName IS NOT NULL) BEGIN EXEC ('ALTER TABLE [TableName] DROP CONSTRAINT ['[email protected]+']') END
If you want to do this for a default column, which is probably more common than the original question and I'm sure a lot of people will land on this from a Google search, then just change the line:
obj.type = 'F'
obj.type = 'D'
There is not a built in procedure to accomplish this, but you can build your own using the information in the information_schema views.
Table based example
Create Proc dropFK(@TableName sysname) as Begin Declare @FK sysname Declare @SQL nvarchar(4000) Declare crsFK cursor for select tu.Constraint_Name from information_schema.constraint_table_usage TU LEFT JOIN SYSOBJECTS SO ON TU.Constraint_NAME = SO.NAME where xtype = 'F' and Table_Name = @TableName open crsFK fetch next from crsFK into @FK While (@@Fetch_Status = 0) Begin Set @SQL = 'Alter table ' + @TableName + ' Drop Constraint ' + @FK Print 'Dropping ' + @FK exec sp_executesql @SQL fetch next from crsFK into @FK End Close crsFK Deallocate crsFK End