DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Douglas has posted 12 posts at DZone. View Full User Profile

Drop An Unnamed Constraint From SqlServer 2000

  • submit to reddit
        // description of your code here

declare @name nvarchar(32), 
    @sql nvarchar(1000)

-- find constraint name
select @name = 
from sysobjects AS O
left join sysobjects AS T
    on O.parent_obj =
where isnull(objectproperty(,'IsMSShipped'),1) = 0
    and not like '%dtproper%'
    and not like 'dt[_]%'
    and = 'MyTable'
    and like 'DF__MyTable__MyColu%'

-- delete if found
if not @name is null
    select @sql = 'ALTER TABLE [MyTable] DROP CONSTRAINT [' + @name + ']'
    execute sp_executesql @sql

-- do your ALTER TABLE here

-- replace the constraint
select @sql = 'ALTER TABLE [MyTable] ADD CONSTRAINT [' + @name + '] DEFAULT (0) FOR [MyColumn]'
execute sp_executesql @sql