日期:2014-05-18  浏览次数:20474 次

text类型中信息的替换问题
使用一下这个方法进行替换但是charindex无法找到我要替换内容的位置。
已经确认要替换的内容确实存在,但是就是找不到。
在使用PATINDEX进行查找时会找到一个位置,但是此位置不是实际对应的位置。无法完成替换
例如PATINDEX的到的数是17000,执行后替换了这个位置的数据,但是我要替换的数据在这个数值之后。
不知道这个数值是怎样找到的,为什么会出现偏差?
求高手解答。。。。。

--SQL Server TEXT类型字段字符串替换示例处理脚本

/*--text字段的替换处理  
  --*/  
  --创建数据测试环境  
--create table #tb(aa text)
declare @s_str varchar(8000),@d_str varchar(8000), --定义替换的字符串 
  @p varbinary(16),@postion int,@rplen int,@i_Start int, @i_End int

select identity(int,1,1) as [id],newsid into # from news
select @i_Start=min([id]),@i_End=max([id]) from #
while (@i_Start<=@i_End)
begin 
  --insert into #tb(aa) select content from # where [id]=@i_Start
  select @s_str='\' --要替换的字符串  
  ,@d_str='!' --替换成的字符串  
  --字符串替换处理  
  select @p=textptr(content),@rplen=len(@s_str),@postion=charindex(@s_str,content)-1 from news where newsid in (select top 1 newsid from # where [id]=@i_Start) 
  while @postion>0  
  begin  
  updatetext news.content @p @postion @rplen @d_str  
  select @postion=charindex(@s_str,content)-1 from news where newsid in (select top 1 newsid from # where [id]=@i_Start)
  end 
  --truncate table #tb
 
  select @i_Start=@i_Start+1
end

------解决方案--------------------
写一个替换函数,论坛里好像是有的,自己搜一下。
------解决方案--------------------
SQL code

[MSSQLServer]Text类型的字符串替换

ntext, text, and image data types will be removed in a future version of MicrosoftSQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead
 
从上文可以看到text,ntext等类型将会被ms sqlserver抛弃,取而代之的是varchar(max)等.
预计也将会取消专门针对text等类型的操作函数,例如textptr,updatetext等。
 
但是目前有许多现存系统仍然存在text类型的字段,因为种种原因已经不能修改数据库结构。
但是我们可以在新写的sql语句及存储过程中采用新的方法,以备将来mssql server抛弃专门针对text等类型的操作函数后修改程序的麻烦。
下面是一个简单的替换例子,
 
针对text类型的字符串替换:
 
设有表 T(id int not null,info text)
要求替换info中的'abc'为'123'
一般的存储过程会写成:
drop procedure dbo.procedure_1
go
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
create procedure dbo.procedure_1
as
declare @ptr varbinary(16)
declare @ID int
declare @Position int,@len int
declare @strsrc char(3)
declare @strdsc char(3)
set @strtmp='abc'
set @strdsc='123'
set @len=3
declare replace_Cursor scroll Cursor
for 
select textptr([info]),id from T
for read only
open replace_Cursor
fetch next from replace_Cursor into @ptr,@ID
while @@fetch_status=0
begin
    select @Position=patindex('%'+@strsrc+'%',[info]) from T where id=@ID
    while @Position>0
    begin
        set @Position=@Position-1
        updatetext T.[info] @ptr @Position @len @strdsc
      select @Position=patindex('%'+@strsrc+'%',[info]) from T where id=@ID
    end
    fetch next from replace_Cursor into @ptr,@ID
end
close replace_Cursor
deallocate replace_Cursor
go

参考
 
其中用到了text专用的函数 updatetext
 
现在我们改写成
drop procedure dbo.procedure_1
go
 
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
 
create procedure dbo.procedure_1
as
 
declare @ID int
declare @strtmp varchar(max)
declare @strsrc char(3),@strdsc char(3)
set @strsrc = 'abc'
set @strdsc = '123'
declare replace_Cursor scroll Cursor
for 
select id from testtable
--for read only
open replace_Cursor
fetch next from replace_Cursor into @ID
while @@fetch_status=0
begin
    select @strtmp = [info] from testtable where id=@ID
    select @strtmp = Replace(@strtmp,@strsrc,@strdsc)
    update T set [info] = @strtmp where id=@ID
    fetch next from replace_Cursor into @ID
end
close replace_Cursor
deallocate replace_Cursor
go
 
这样,无论info字段改成char,nchar,text都好,一样均可通用