批量修改系统中int,varchar字段的默认值
2010-08-12 19:29
295 查看
declare @t table(id int identity(1,1),tbname varchar(256), colname varchar(256),xtype varchar(20))
insert into @t
select a.name,b.name ,c.name
from sysobjects a
inner join syscolumns b on a.id=b.id
inner join systypes c on b.xusertype = c.xusertype
where a.xtype='u'
and c.name in ('varchar','int')
and b.status<>0x80 --去掉自增列
and not exists --过滤掉原来已存在默认值的列
(select 1
from
(select
(select name from sysobjects where id=c.id) 表名,
(select name from syscolumns where cdefault=a.id) 字段名
from sysobjects b,syscolumns c,syscomments a
where b.xtype='d'
and a.id=b.id
and b.parent_obj=c.id
and a.colid=c.colid
) t
where a.name=t.表名
and b.name=t.字段名)
--select * from @t
declare @i int
set @i=1
declare @tbname varchar(256),@colname varchar(256),@xtype varchar(20),@sql nvarchar(4000)
while @i <= (select MAX(id) from @t)
begin
select @tbname=tbname,@colname=colname,@xtype = xtype from @t where id=@i
set @sql = 'alter table ['+@tbname+'] add constraint ' + 'df_' + replace(@tbname,'-','') +'_'+ replace(@colname,'-','') + ' default '
if @xtype = 'int'
begin
set @sql = @sql + ' 0 '
end
else if @xtype = 'varchar'
begin
set @sql = @sql + ''''''
end
set @sql = @sql + ' for [' + @colname +']'
exec(@sql)
set @i = @i + 1
end
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/htl258/archive/2009/04/16/4083008.aspx
insert into @t
select a.name,b.name ,c.name
from sysobjects a
inner join syscolumns b on a.id=b.id
inner join systypes c on b.xusertype = c.xusertype
where a.xtype='u'
and c.name in ('varchar','int')
and b.status<>0x80 --去掉自增列
and not exists --过滤掉原来已存在默认值的列
(select 1
from
(select
(select name from sysobjects where id=c.id) 表名,
(select name from syscolumns where cdefault=a.id) 字段名
from sysobjects b,syscolumns c,syscomments a
where b.xtype='d'
and a.id=b.id
and b.parent_obj=c.id
and a.colid=c.colid
) t
where a.name=t.表名
and b.name=t.字段名)
--select * from @t
declare @i int
set @i=1
declare @tbname varchar(256),@colname varchar(256),@xtype varchar(20),@sql nvarchar(4000)
while @i <= (select MAX(id) from @t)
begin
select @tbname=tbname,@colname=colname,@xtype = xtype from @t where id=@i
set @sql = 'alter table ['+@tbname+'] add constraint ' + 'df_' + replace(@tbname,'-','') +'_'+ replace(@colname,'-','') + ' default '
if @xtype = 'int'
begin
set @sql = @sql + ' 0 '
end
else if @xtype = 'varchar'
begin
set @sql = @sql + ''''''
end
set @sql = @sql + ' for [' + @colname +']'
exec(@sql)
set @i = @i + 1
end
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/htl258/archive/2009/04/16/4083008.aspx
相关文章推荐
- 批量修改系统中int,varchar字段的默认值
- 批量修改系统中int,varchar字段的默认值
- 批量修改sqlserver 字段默认值
- oracle向已有表增加字段、字段设置默认值、修改表字varchar类型的长度
- 批量修改字段的类型(SQL Server)--varchar到nvarchar
- 批量修改字段的类型(SQL Server)--varchar到nvarchar
- SQL SERVER 2005 修改字段的默认值
- pg批量修改字段内容
- linux系统批量修改root用户密码
- SQL替换语句之批量修改、增加、删除字段内容
- mysql修改表中某个字段的默认值
- MySQL修改字段默认值
- MySQL 日期类型及默认设置 (除timestamp类型外,系统不支持其它时间类型字段设置默认值)
- 批量修改字段长度,考虑主键外键索引的情况
- MySQL语句增加字段,修改字段名,修改类型,修改默认值
- 根据字段关联批量修改关联数据
- 批量把自己系统的数据字典写到数据库字段的说明
- Sql Server 增加字段、修改字段、修改类型、修改默认值
- Django中 如何修改models字段的默认值
- android4.0.4 系统默认值的修改