---脚本适用于sql 2005 sp3及以上
declare @sql nvarchar(max),@search nvarchar(100),@update nvarchar(100)
set @search='张3'
set @update='李4'
--set @sql='declear @sql nvarchar(4000)'
--select @sql='select @sql=''select '''
set @sql=''
select @sql=@sql+'update '+sysobjectsname+' set '+syscolumnsname+'='''+@update+''' where '+syscolumnsname+'='''+@search+''' ' from sysobjects inner join syscolumns on sysobjectsid=syscolumnsid where sysobjectsxtype='U' and syscolumnsxtype in (35,99,167,175,231,239)
exec sp_executesql @sql
CREATE procedure sp_columnsearch
@search varchar(100),
@update varchar(100)=''
as
begin
---脚本适用于sql 2005 sp3及以上
declare @sql nvarchar(max)
select @sql=@sql+'update '+sysobjectsname+' set '+syscolumnsname+'='''+@update+''' where '+syscolumnsname+'='''+@search+''' ' from sysobjects inner join syscolumns on sysobjectsid=syscolumnsid where sysobjectsxtype='U' and syscolumnsxtype in (35,99,167,175,231,239)
exec sp_executesql @sql
end
经检查,没有问题,除非选错了库运行或者
update aaa set bb='abc' where bb='123'
在你的库里面本来就不能匹配
update
A
set
C=cast(rand()
as
varchar(4))
where
B=7
关键在于两个:
cast(rand()
as
varchar(4))
如果C的类型是float,替换成rand()
如果需要每个随机数都不同,那就需要有一个id列,然后上面替换成cast(rand(id)
as
varchar(4))
假设表名叫t
update t set b=t2b from t t1 inner join (select a,max(b) b from t group by a) t2 on t1a=t2a数据表位A_ziliao 有一标志id
首先建立一个过渡表(id,地名)
用游标查询数据表A_ziliao记录id
更改列 sheng 为随机值
declare @fid int,@nn int
declare cursor3 cursor for --定义游标cursor3
select id from A_ziliao --使用游标的对象(跟据需要填入select文)
open cursor3 --打开游标
fetch next from cursor3 into @fid
while @@fetch_status=0 --判断是否成功获取数据
begin
set @nn =4rand +1'随机id
update A_ziliao set sheng=(select 地名 from 过渡表 where id= @nn )where id=@fid
fetch next from cursor3 into @fid --将游标向下移1行
end
close cursor3 --关闭游标
deallocate cursor3
update cm_zd_nxb set xm_code = substr(xm_code,2,2) where xm_code like '0%'
看看是不是你想要的
--或者这样update cm_zd_nxb set xm_code = ltrim(xm_code,'0') where xm_code like '0%'
----先删除要处理字段的默认值约束
declare @s varchar(8000)
set @s=''
select @s=@s+'
alter table ['+bname+'] drop constraint ['+dname+']'
from syscolumns a
join sysobjects b on aid=bid
join syscomments c on acdefault=cid
join sysobjects d on cid=did
where bname='KeyWords'
and (aname='hits')
exec(@s)
----如果要要将 hits 字段改为数字类型
alter table KeyWords alter column [hits] numeric(18,0)
----再为字段 hits 添加默认值约束
set @s='alter table KeyWords add constraint
[df__t1__hits__'+cast(newid() as varchar(36))
+'] default (0) for hits'
exec(@s)
go
--如果要改已有的数据
update KeyWords set hits=0 where 1=1
以上就是关于sql 批量修改数据全部的内容,包括:sql 批量修改数据、sql2000 数据批量修改、sql批量修改数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)