This is based on an article in BOL "Working with Default Constraints" worth a read
DECLARE @defname VARCHAR(100), @cmd VARCHAR(1000)
SET @defname =
(SELECT name
FROM sysobjects so JOIN sysconstraints sc
ON so.id = sc.constid
WHERE object_name(so.parent_obj) = 'MyTable'
AND so.xtype = 'D'
AND sc.colid =
(SELECT colid FROM syscolumns
WHERE id = object_id('dbo.MyTable') AND
name = 'MyColumn'))
SET @cmd = 'ALTER TABLE MyTable DROP CONSTRAINT '
+ @defname
EXEC(@cmd)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.