declare @chvField nvarchar(200) declare cur_test cursor forselect spc from prdt open cur_test fetch next from cur_test into @chvField while @@fetch_status=0 begin update prdt set spc=replace(@chvField ,'AAAA','BBBB') where spc=@chvField fetch next from cur_test into @chvField end close cur_test deallocate cur_test前提是spc唯一,如果不唯一where后面可以用其他唯一字段代替
如果是text类型:--定义替换的字符串 declare @s_str varchar(8000),@d_str varchar(8000) select @s_str='a' --要替换的字符串 ,@d_str='ab' --替换成的字符串 --因为只能用patindex,所以对于搜索字符串做处理 set @s_str='%'+@s_str+'%'--定义游标,循环处理数据 declare @id varchar(3) declare #tb cursor for select id from test open #tb fetch next from #tb into @id while @@fetch_status=0 begin --字符串替换处理 declare @p varbinary(16) ,@p1 int,@p2 int ,@rplen int,@step int,@len int select @p=textptr(txt) ,@rplen=len(@s_str)-2 ,@step=len(@d_str) ,@p1=patindex(@s_str,txt) ,@len=datalength(txt) ,@p2=0 from test where id=@id
while @p1>0 begin set @p2=@p1+@p2-1 updatetext test.txt @p @p2 @rplen @d_str select @p2=@p2+1,@p1=patindex(@s_str,substring(txt,@p2+1,@len)) from test where id=@id end fetch next from #tb into @id end close #tb deallocate #tb--显示结果 select datalength(txt),* from testgo --删除数据测试环境 drop table test
SELECT REPLACE(spc字段,'cde','xxx') from your_tablename
如果你修改 update your_tablename set spc字段 = REPLACE(spc字段,'cde','xxx')
TO 631799(杭州工人) 不好意思,首先说一下我不会sql,所以问的问题可能幼稚了点,请见谅啊你的语句中的id指的是什么?我理解为主键了不知道是不是。txt指的是我所说的spc字段吧 我按照我以上的理解把你提供的语句执行成功了,可是为什么我什么效果都没有看到呢?因为prdt表的spc字段中有大量的纪录中都有AAA,所以我的意思是要批量改,但一般含此文字的纪录其主键是连续的,所以更改语句才能再限制的主键范围内更改呢? 谢谢!
declare cur_test cursor forselect spc from prdt
open cur_test
fetch next from cur_test into @chvField
while @@fetch_status=0
begin
update prdt set spc=replace(@chvField ,'AAAA','BBBB') where spc=@chvField
fetch next from cur_test into @chvField
end
close cur_test
deallocate cur_test前提是spc唯一,如果不唯一where后面可以用其他唯一字段代替
如果是text类型:--定义替换的字符串
declare @s_str varchar(8000),@d_str varchar(8000)
select @s_str='a' --要替换的字符串
,@d_str='ab' --替换成的字符串
--因为只能用patindex,所以对于搜索字符串做处理
set @s_str='%'+@s_str+'%'--定义游标,循环处理数据
declare @id varchar(3)
declare #tb cursor for select id from test
open #tb
fetch next from #tb into @id
while @@fetch_status=0
begin
--字符串替换处理
declare @p varbinary(16)
,@p1 int,@p2 int
,@rplen int,@step int,@len int select @p=textptr(txt)
,@rplen=len(@s_str)-2
,@step=len(@d_str)
,@p1=patindex(@s_str,txt)
,@len=datalength(txt)
,@p2=0
from test where id=@id
while @p1>0
begin
set @p2=@p1+@p2-1
updatetext test.txt @p @p2 @rplen @d_str
select @p2=@p2+1,@p1=patindex(@s_str,substring(txt,@p2+1,@len))
from test where id=@id
end
fetch next from #tb into @id
end
close #tb
deallocate #tb--显示结果
select datalength(txt),* from testgo
--删除数据测试环境
drop table test
回复人: goregrypeck(派克) ( ) 信誉:100 2005-01-04 12:06:00 得分: 0
SELECT REPLACE(spc字段,'cde','xxx') from your_tablename
update your_tablename
set spc字段 = REPLACE(spc字段,'cde','xxx')
不好意思,首先说一下我不会sql,所以问的问题可能幼稚了点,请见谅啊你的语句中的id指的是什么?我理解为主键了不知道是不是。txt指的是我所说的spc字段吧
我按照我以上的理解把你提供的语句执行成功了,可是为什么我什么效果都没有看到呢?因为prdt表的spc字段中有大量的纪录中都有AAA,所以我的意思是要批量改,但一般含此文字的纪录其主键是连续的,所以更改语句才能再限制的主键范围内更改呢?
谢谢!
支持批量改的啊我这种方法只适会字段类型为text型的,如果为其它类型,则用楼上其它的。
update prdt set spc=REPLACE(spc,'AAAA','BBBB')
id --主键
txt --要替换的列(text类型)只要把把相应的换掉你实际的就行了。
update prdt set spc = replace(spc,'AAA','BBB')