举例:
create table test(id int identity(1,1),content text)
insert test values('asdfsdfasdfsadfsdjflk大力sdjflkasjdf;lasdjf;lafsd')
insert test values('fdsgdsfgdfgsdfghrtjtyjkhkhjkhjljk大力ljk;jk;kl;kl;kl;')
gocreate proc 替换
@s_str varchar(100),
@d_str varchar(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
goexec 替换 '大力','AAAAA'goselect * from testgo
drop table test
  

解决方案 »

  1.   

    text字段的替换处理
    http://expert.csdn.net/Expert/topic/2236/2236811.xml?temp=2.662295E-02主题:text字段的替换处理
    问题:要将text类型字段中的字串 123 用000替换--创建数据测试环境
    create table #tb(aa text)
    insert into #tb select 'abc123abc123,asd'--定义替换的字符串
    declare @s_str varchar(8000),@d_str varchar(8000)
    select @s_str='123' --要替换的字符串
    ,@d_str='000' --替换成的字符串--字符串替换处理
    declare @p varbinary(16),@postion int,@rplen int
    select @p=textptr(aa),@rplen=len(@s_str),@postion=charindex(@s_str,aa)-1 from #tb
    while @postion>0
    begin
    updatetext #tb.aa @p @postion @rplen @d_str
    select @postion=charindex(@s_str,aa)-1 from #tb
    end--显示结果
    select * from #tb--删除数据测试环境
    drop table #tb/****************全部替换************************/
    DECLARE @ptrval binary(16)
    SELECT @ptrval = TEXTPTR(aa)  FROM  #tb  WHERE aa like '%数据2%'
    if @ptrval is not null        -- 一定要加上此句,否则若找不到数据下一句就会报错
    UPDATETEXT #tb.aa @ptrval 0 null '数据3'
    /****************在字段尾添加**********************************/
    --定义添加的的字符串
    declare @s_str varchar(8000)
    select @s_str='*C'  --要添加的字符串
    --字符串添加处理
    declare @p varbinary(16),@postion int,@rplen int
    select @p=textptr(detail) from test where id='001'
    updatetext test.detail @p null null @s_str总结:
    1:Text字段类型不能直接用replace函数来替换,必须用updatetext
    2:字段比较不能用 where 字段 = ‘某数据’,可以用like来代替
    3:updatetext时,若@ptrval值为空会出错,需注意。
      

  2.   

    create table test(id int identity(1,1),content text)
    insert test values(',0,1,2,3,')
    insert test values(',0,1,2,3,')
    gocreate proc 替换
    @s_str varchar(100),
    @d_str varchar(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
    goexec 替换 ',0,',',A,'goselect * from testgo
    drop table test
      

  3.   

    --如果楼主只要替换一条数据可以不要游标举例:
    create table test(id int identity(1,1),content text)
    insert test values(',0,1,2,3,')
    insert test values(',0,1,2,3,')
    gocreate proc 替换
    @s_str varchar(100),
    @d_str varchar(100),
    @id int
    as
      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
    goexec 替换 ',0,',',A,',1goselect * from testgo
    drop table test
      

  4.   

    --创建数据测试环境
    create table #tb(aa text)
    insert into #tb select ',0,1,2,3,'--定义替换的字符串
    declare @s_str varchar(8000),@d_str varchar(8000)
    select @s_str=',0,' --要替换的字符串
    ,@d_str=',a,' --替换成的字符串--字符串替换处理
    declare @p varbinary(16),@postion int,@rplen int
    select @p=textptr(aa),@rplen=len(@s_str),@postion=charindex(@s_str,aa)-1 from #tb
    while @postion>0
    begin
    updatetext #tb.aa @p @postion @rplen @d_str
    select @postion=charindex(@s_str,aa)-1 from #tb
    end--显示结果
    select * from #tb--删除数据测试环境
    drop table #tb
      

  5.   

    --text字段的替换处理--全表替换--创建数据测试环境
    create table test(id varchar(3),txt text)
    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=',0,'  --要替换的字符串
    ,@d_str=',a,' --替换成的字符串
    --定义游标,循环处理数据
    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--显示结果
    select * from testgo
    --删除数据测试环境
    drop table test