有一个表T(ID,Context),其中Context为text类型,我想把里面的‘abccs'全部替换成‘11011',请问怎么实现。

解决方案 »

  1.   

    --如果长度小于8000
    update tb set Context=replace(cast(Context as varchar),'abccs','11011')
      

  2.   

    --如果长度大于8000,用如下语句  Declare   @s_str   varchar(8000),  @d_str  varchar(8000)   
      Select   @s_str='abccs' ,   --要替换的字符串   
               @d_str='11011'     --替换成的字符串      Declare @p varbinary(16),@postion   int,@rplen   int   
       Select  @p=textptr(Context),@rplen=len(@s_str),@postion=charindex(@s_str,Context)-1 from tb   
      while @postion>0   
      begin   
         updatetext tb.Context  @p   @postion   @rplen   @d_str   
         select   @postion=charindex(@s_str,Context)-1   from tb   
      end
      

  3.   

    --如果长度大于8000,用如下语句  Declare   @s_str   varchar(8000),  @d_str  varchar(8000)   
      Select   @s_str='abccs' ,   --要替换的字符串   
               @d_str='11011'     --替换成的字符串      Declare @p varbinary(16),@postion   int,@rplen   int   
       Select  @p=textptr(Context),@rplen=len(@s_str),@postion=charindex(@s_str,Context)-1 from tb   
      while @postion>0   
      begin   
         updatetext tb.Context  @p   @postion   @rplen   @d_str   
         select   @postion=charindex(@s_str,Context)-1   from tb   
      end===================================================================
    这段不执行,但是没有语法错误,不知道原因,请指教.
      

  4.   

    Declare   @s_str   varchar(8000),  @d_str  varchar(8000)   
      Select   @s_str='abccs' ,   --要替换的字符串   
               @d_str='11011'     --替换成的字符串      Declare @p varbinary(16),@postion   int,@rplen   int   
       Select  @p=textptr(Context),@rplen=len(@s_str),@postion=charindex(@s_str,Context)-1 from tb   
      while @postion >= 0         -------------------------------修改地方------  
      begin   
         updatetext tb.Context  @p   @postion   @rplen   @d_str   
         select   @postion=charindex(@s_str,Context)-1   from tb   
      end