sql ntext 替换存储过程

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

create procedure replace_ntext          
  @table_name    nvarchar(100),   --目标表名      
  @field_name    nvarchar(100),   --目标字段名      
  @where_str        nvarchar(1000),   --该表主键字段名  如id=1     
  @old    nvarchar(1000),        --需要替换的字符串      
  @new    nvarchar(1000)         --替换后的字符串         
as     
begin  
   
declare @SqlStr nvarchar(4000)    
	set @SqlStr = 'declare @pos int, @len int, @count int  set @count=0  set @len=len('''+@old+''') '
	set @SqlStr = @SqlStr + ' select @pos = patINDEX(''%'+@old+'%'',['+@field_name+']) - 1'+' from ['+@table_name+'] where '+ @where_str
	set @SqlStr = @SqlStr + ' while @pos>0 ' 
	set @SqlStr = @SqlStr + ' begin'   
	set @SqlStr = @SqlStr + ' declare @ptrval binary(16)'      
	set @SqlStr = @SqlStr + ' SELECT  @ptrval  =  TEXTPTR(['+@field_name+'])   from ['+@table_name+'] where '+  @where_str
	set @SqlStr = @SqlStr + ' UPDATETEXT ['+@table_name+'].['+@field_name+'] @ptrval  @pos  @len '''+ @new + ''''
	set @SqlStr = @SqlStr + ' select @pos = patINDEX(''%'+@old+'%'',['+@field_name+']) - 1'+' from ['+@table_name+'] where '+ @where_str
	set @SqlStr = @SqlStr + ' set  @count=@count+1 '
	set @SqlStr = @SqlStr + ' end '
	set @SqlStr = @SqlStr + ' select @count'
      --print @SqlStr
        execute sp_executesql   @SqlStr           
end