Skip to content

Instantly share code, notes, and snippets.

@Solstice1557
Created May 20, 2014 05:58
Show Gist options
  • Save Solstice1557/cd151fdf4446f8495ce9 to your computer and use it in GitHub Desktop.
Save Solstice1557/cd151fdf4446f8495ce9 to your computer and use it in GitHub Desktop.
Drop column with constraints
DECLARE @sql NVARCHAR(MAX)
WHILE 1=1
BEGIN
SELECT TOP 1 @sql = N'alter table TABLE_NAME drop constraint ['+dc.NAME+N']'
from sys.default_constraints dc
JOIN sys.columns c
ON c.default_object_id = dc.object_id
WHERE
dc.parent_object_id = OBJECT_ID('TABLE_NAME')
AND c.name = N'COLUMN_NAME'
IF @@ROWCOUNT = 0 BREAK
EXEC (@sql)
END
GO
alter table TABLE_NAME drop column COLUMN_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment