能否用一段script语句将数据库中所有表的某种类型字段全部设置成默认值,如int类型
我遇到的情况是整个数据库中很多表,里面有N多int字段的,怎么将这些int字段的默认值全部设置成0
---------------------------------------------------------------
declare @sql varchar(8000),@tb varchar(100)
declare c cursor for
select name from sysobjects where xtype='U' and name<>'dtproperties'
open c
fetch next from c into @tb
while @@fetch_status=0
begin
set @sql=''
select @sql=@sql+'alter table '+a.name+' add constraint df_'+a.name+''+b.name+' default(0) for '+b.name+char(13)
from sysobjects a,syscolumns b
where a.id=b.id and a.xtype='U' and a.name<>'dtproperties' and b.xtype=56
and a.name=@tb and COLUMNPROPERTY(b.id,b.name,'IsIdentity')<>1
and 'df'+a.name+'_'+b.name not in(select name from sysobjects where xtype='D')
if @sql<>''
exec(@sql)
fetch next from c into @tb
end
close c
deallocate c