text/ntext字段的替换处理--全表替换
--text/ntext字段的替换处理--全表替换
--SELECT * FROM #tb
/*
--创建数据测试环境
create table test(id varchar(3),txt ntext)
insert into test
select '001','A*B'
union all select '002','A*B-AA*BB'
go
*/
--定义替换的字符串
declare @s_str varchar(8000),@d_str varchar(8000)
select @s_str='www.leods.com' --要替换的字符串
,@d_str='www.4wdcc.com' --替换成的字符串
--定义游标,循环处理数据
declare @id varchar(12)
declare #tb cursor for select id from ta_news
open #tb
fetch next from #tb into @id
while @@fetch_status=0
begin
--字符串替换处理
declare @p varbinary(16),@postion int,@rplen int
select @p=textptr([Content])
,@rplen=len(@s_str)
,@postion=charindex(@s_str,[Content])-1
from ta_news where id=@id
while @postion>0
begin
updatetext ta_news.[Content] @p @postion @rplen @d_str
select @postion=charindex(@s_str,[Content])-1 from ta_news where id=@id
end
fetch next from #tb into @id
end
close #tb
deallocate #tb
/*
--显示结果
select * from test
go
--删除数据测试环境
drop table test
*/
--SELECT * FROM #tb
/*
--创建数据测试环境
create table test(id varchar(3),txt ntext)
insert into test
select '001','A*B'
union all select '002','A*B-AA*BB'
go
*/
--定义替换的字符串
declare @s_str varchar(8000),@d_str varchar(8000)
select @s_str='www.leods.com' --要替换的字符串
,@d_str='www.4wdcc.com' --替换成的字符串
--定义游标,循环处理数据
declare @id varchar(12)
declare #tb cursor for select id from ta_news
open #tb
fetch next from #tb into @id
while @@fetch_status=0
begin
--字符串替换处理
declare @p varbinary(16),@postion int,@rplen int
select @p=textptr([Content])
,@rplen=len(@s_str)
,@postion=charindex(@s_str,[Content])-1
from ta_news where id=@id
while @postion>0
begin
updatetext ta_news.[Content] @p @postion @rplen @d_str
select @postion=charindex(@s_str,[Content])-1 from ta_news where id=@id
end
fetch next from #tb into @id
end
close #tb
deallocate #tb
/*
--显示结果
select * from test
go
--删除数据测试环境
drop table test
*/
浙公网安备 33010602011771号