Never been to DZone Snippets before?

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

About this user

Douglas Wyatt

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Drop an unnamed constraint from SqlServer 2000

// description of your code here

   1  
   2  declare @name nvarchar(32), 
   3      @sql nvarchar(1000)
   4  
   5  -- find constraint name
   6  select @name = O.name 
   7  from sysobjects AS O
   8  left join sysobjects AS T
   9      on O.parent_obj = T.id
  10  where isnull(objectproperty(O.id,'IsMSShipped'),1) = 0
  11      and O.name not like '%dtproper%'
  12      and O.name not like 'dt[_]%'
  13      and T.name = 'MyTable'
  14      and O.name like 'DF__MyTable__MyColu%'
  15  
  16  -- delete if found
  17  if not @name is null
  18  begin
  19      select @sql = 'ALTER TABLE [MyTable] DROP CONSTRAINT [' + @name + ']'
  20      execute sp_executesql @sql
  21  end
  22  
  23  -- do your ALTER TABLE here
  24  
  25  -- replace the constraint
  26  select @sql = 'ALTER TABLE [MyTable] ADD CONSTRAINT [' + @name + '] DEFAULT (0) FOR [MyColumn]'
  27  execute sp_executesql @sql
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS