日期:2014-05-18  浏览次数:20608 次

批量改变数据类型,在线等,马上结贴
数据库中有很多表的类型是float型的,现在都要改成varchar型,如果用alter   table   tblname   alter   column   colname   varchar(20),这样太慢了,数据库中有很多float类型要改变,请问下可以通过修改系统表,可以改变吗?

------解决方案--------------------
declare @tbname varchar(40),@colname varchar(40),@sql varchar(400)
declare cur cursor for
select b.name tbname,a.name colname from syscolumns a inner join sysobjects b on a.id=b.id
where b.xtype= 'u ' and b.status> 0 and a.xusertype=62
open cur
fetch next from cur into @tbname,@colname
while @@fetch_status=0
begin
set @sql= 'alter table '+@tbname+ ' alter column '+@colname+ ' varchar(20) '
exec( @sql)
fetch next from cur into @tbname,@colname
end
deallocate cur
go
------解决方案--------------------
----遍历所有float类型,生成修改类型的SQL语句字符串
declare @str varchar(1000)
set @str = 'alter table tblname alter column '
select @str = @str + name + ' varchar(20) ;alter table tbtest alter column ' from syscolumns where id = object_id( 'tblname ') and xtype = 62
----去掉最后多余的 "alter table tbtest alter column "
set @str = reverse(stuff(reverse(@str),1,charindex( '; ',reverse(@str)), ' '))
----查看SQL字符串
print @str
----执行修改
exec(@str)
------解决方案--------------------
楼上的是用游标实现的,可以用修改数据库系统表的办法吗
=====>
create table t(col int)
insert into t values(33)
go
exec sp_configure 'allow updates ',1
go
reconfigure with override
go
update syscolumns set xtype=231,xusertype=231 where id=object_id( 't ')
go
insert into t values( 'ab ')
select * from t

drop table t

--说明:此种方法极其危险,劝楼主还是老老实实