--定义游标,循环处理数据
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),@postion int,@rplen int
select @p=textptr(txt)
,@rplen=len(@s_str)
,@postion=charindex(@s_str,txt)-1
from test where id=@id
while @postion>0
begin
updatetext test.txt @p @postion @rplen @d_str
select @postion=charindex(@s_str,txt)-1 from test where id=@id
end
fetch next from #tb into @id
end
close #tb
deallocate #tb
create table test(id int identity(1,1),content ntext)
insert test values(N'asdfsdfasdfsadfsdjflk大力sdjflkasjdf;lasdjf;lafsd')
insert test values(N'fdsgdsfgdfgsdfghrtjtyjkhkhjkhjljk大力ljk;jk;kl;kl;kl;')
go
create proc 替换
@s_str nvarchar(100),
@d_str nvarchar(100)
as
declare @id int
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),@postion int,@rplen int
select @p=textptr(CONTENT),@rplen=len(@s_str),@postion=charindex(@s_str,CONTENT)-1 from test where id=@id
while @postion>0
begin
updatetext test.CONTENT @p @postion @rplen @d_str
select @postion=charindex(@s_str,content)-1 from test where id=@id
end
fetch next from #tb into @id
end
close #tb
deallocate #tb
go
USE pubs
GO
DECLARE @ptrval varbinary(16)
SELECT @ptrval = TEXTPTR(pr_info)
FROM pub_info pr INNER JOIN publishers p
ON pr.pub_id = p.pub_id
AND p.pub_name = 'New Moon Books'
READTEXT pub_info.pr_info @ptrval 1 25
GO