SQL text字段的替换处理(无法批量更新,写法记录)

注: SQL2005以上版本直接使用varchar(max)的replace即可

此处只是记录一种写法

 

/* --text字段的替换处理 
--
*/ 
-- 创建数据测试环境 
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 

你可能感兴趣的:(text)