exec('alter table '+@表名+' drop constraint '+@name) 
exec('alter  table  '+@表名+'  add  constraint  df_'+@表名+@列名+' default  '''+@默认值+'''  for  '+@列名) 

解决方案 »

  1.   

    --原來狀態
    alter table test_tb add col1 int default(1)
    --去掉默認屬性
    alter table test_tb alter column col1 int null
      

  2.   

    /*--修改/删除有默认值的字段 示例--*/--测试表
    Create Table t1(
    ID  int  Default(0),
    Name Varchar(16)  Default(16),
    Memo Varchar(255) Default('')
    )
    go--要求,删除name字段,将memo字段改为: Memo Varchar(32) Default(32)--处理方法----a.先删除要处理字段的默认值约束
    declare @s varchar(8000)
    set @s=''
    select @s=@s+'
    alter table ['+b.name+'] drop constraint ['+d.name+']'
    from syscolumns a
    join sysobjects b on a.id=b.id
    join syscomments c on a.cdefault=c.id
    join sysobjects d on c.id=d.id
    where b.name='t1' 
    and (a.name='name' or a.name='memo')
    exec(@s)----b.再删除字段 name
    alter table t1 drop column [name]----c.修改字段 memo
    alter table t1 alter column [memo] varchar(32)----d.为字段 memo 添加默认值约束
    set @s='alter table t1 add constraint
    [df__t1__memo__'+cast(newid() as varchar(36))
    +'] default (32) for memo'
    exec(@s)
    go--插入数据测试
    insert t1 values(default,default)
    select * from t1
    go--删除测试
    drop table t1/*--测试结果
    ID          Memo                             
    ----------- -------------------------------- 
    0           32(所影响的行数为 1 行)
    --*/