第1个回答 2010-05-13
ALTER TABLE 表 ADD CONSTRAINT 约束名称 DEFAULT 默认值 FOR 字段
--------------------
批量修改:
只需把 set @table = '模拟表'改成你的表名后执行,就可以把表中所有数字类型字段初始值改成0
declare @table varchar(255)
declare @sql varchar(8000),@linkword varchar(10)
set @table = '模拟表'
set @sql = ''
select @linkword = case when @linkword is null then '' else '; ' end,@sql = @sql + @linkword + 'ALTER TABLE dbo.'+@table
+' ADD CONSTRAINT DF_'+@table+'_'+col+' DEFAULT 0 FOR '+col
from(
select c.name as col
from syscolumns c join systypes t
on c.xtype=t.xtype
where (t.name like '%int%' or t.name in('decimal','numeric','float','real') or t.name like '%money%') and c.id=object_id(@table)) colname
exec(@sql)