一、问题描述:
1。在sql Server 中,ntext/text/image 字段不允许应用replace函数替换内容;
2。通过convert字段转换,可以把ntext字段转换为varchar(8000),然后用Relpace函数替换,不过,此方法,对于字段长度大于8000的ntext字段无效。
二、问题解决
整理通用存储过程,代码如下: 代码如下:
CREATE procedure [dbo].[Proc_UpdateNTextField]
@TargetTable nvarchar(1000),--目标表名
@TargetField nvarchar(1000),--目标字段名
@PKField nvarchar(1000),--该表主键字段名
@otxt nvarchar(1000),--需要替换的字符串
@ntxt nvarchar(1000) --替换后的字符串
as
begin
declare @sqlStr nvarchar(4000)
set @sqlStr = ' declare @txtlen int '
set @sqlStr = @sqlStr + ' set @txtlen = len(''' + @otxt + ''') '
set @sqlStr = @sqlStr + ' declare @pos int '
set @sqlStr = @sqlStr + ' set @pos = 0 '
set @sqlStr = @sqlStr + 'declare curs cursor local fast_forward for select '
set @sqlStr = @sqlStr + @PKField + ',textptr(' + @TargetField +') from ' + @TargetTable +' where ' + @TargetField + ' like ''%' + @otxt +'%'''
set @sqlStr = @sqlStr + ' declare @ptr binary(16) '
set @sqlStr = @sqlStr + ' declare @id char(32) '
set @sqlStr = @sqlStr + ' open curs '
set @sqlStr = @sqlStr + ' fetch next from curs into @id,@ptr '
set @sqlStr = @sqlStr + ' while @@fetch_status = 0 '
set @sqlStr = @sqlStr + ' begin '
set @sqlStr = @sqlStr + ' select @pos= patindex(''%' + @otxt + '%'',ProductDesc) from ProductTemp where ProductID=@id '
set @sqlStr = @sqlStr + ' while @pos>0 '
set @sqlStr = @sqlStr + ' begin ' set @sqlStr = @sqlStr + ' set @pos=@pos-1 '
set @sqlStr = @sqlStr + ' updatetext ' + @TargetTable + '.' +@TargetField + ' @ptr @pos @txtlen ''' + @ntxt + ''' '
set @sqlStr = @sqlStr + ' select @pos= patindex(''%' + @otxt + '%'',ProductDesc) from ProductTemp where ProductID=@id '
set @sqlStr = @sqlStr + ' end '
set @sqlStr = @sqlStr + ' fetch next from curs into @id,@ptr '
set @sqlStr = @sqlStr + ' end '
set @sqlStr = @sqlStr + ' close curs '
set @sqlStr = @sqlStr + ' deallocate curs '
EXECUTE sp_executesql @sqlStr
end
原文链接:https://www.f2er.com/mssql/64283.html@TargetTable nvarchar(1000),--目标表名
@TargetField nvarchar(1000),--目标字段名
@PKField nvarchar(1000),--该表主键字段名
@otxt nvarchar(1000),--需要替换的字符串
@ntxt nvarchar(1000) --替换后的字符串
as
begin
declare @sqlStr nvarchar(4000)
set @sqlStr = ' declare @txtlen int '
set @sqlStr = @sqlStr + ' set @txtlen = len(''' + @otxt + ''') '
set @sqlStr = @sqlStr + ' declare @pos int '
set @sqlStr = @sqlStr + ' set @pos = 0 '
set @sqlStr = @sqlStr + 'declare curs cursor local fast_forward for select '
set @sqlStr = @sqlStr + @PKField + ',textptr(' + @TargetField +') from ' + @TargetTable +' where ' + @TargetField + ' like ''%' + @otxt +'%'''
set @sqlStr = @sqlStr + ' declare @ptr binary(16) '
set @sqlStr = @sqlStr + ' declare @id char(32) '
set @sqlStr = @sqlStr + ' open curs '
set @sqlStr = @sqlStr + ' fetch next from curs into @id,@ptr '
set @sqlStr = @sqlStr + ' while @@fetch_status = 0 '
set @sqlStr = @sqlStr + ' begin '
set @sqlStr = @sqlStr + ' select @pos= patindex(''%' + @otxt + '%'',ProductDesc) from ProductTemp where ProductID=@id '
set @sqlStr = @sqlStr + ' while @pos>0 '
set @sqlStr = @sqlStr + ' begin ' set @sqlStr = @sqlStr + ' set @pos=@pos-1 '
set @sqlStr = @sqlStr + ' updatetext ' + @TargetTable + '.' +@TargetField + ' @ptr @pos @txtlen ''' + @ntxt + ''' '
set @sqlStr = @sqlStr + ' select @pos= patindex(''%' + @otxt + '%'',ProductDesc) from ProductTemp where ProductID=@id '
set @sqlStr = @sqlStr + ' end '
set @sqlStr = @sqlStr + ' fetch next from curs into @id,@ptr '
set @sqlStr = @sqlStr + ' end '
set @sqlStr = @sqlStr + ' close curs '
set @sqlStr = @sqlStr + ' deallocate curs '
EXECUTE sp_executesql @sqlStr
end