sql server中批量替换大于8000的ntext字段内容
[ 2010-12-16 09:40:56 | 作者: admin ]
(1)、varchar和nvarchar类型是支持replace,所以如果你的text/ntext不超过8000/4000可以先转换成前面两种类型再使用replace。
(2)、大于8000的需要用存储过程
调用演示
存储过程ReplaceText
CREATE procedure [dbo].[ReplaceText]
@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 + '%'', ' + @TargetField + ') from ' + @TargetTable +' where '+@PKField+'=@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 + '%'', ' + @TargetField + ') from ' + @TargetTable +' where '+@PKField+'=@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
GO
评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=1611
update 表名 set text类型字段名= replace ( convert ( varchar ( 8000) , text类型字段名) , '要替换的字符' , '替换成的值' )
(2)、大于8000的需要用存储过程
调用演示
exec replacenew 'info' , 'content','id' ,'sohu.com','52news.com'
存储过程ReplaceText
CREATE procedure [dbo].[ReplaceText]
@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 + '%'', ' + @TargetField + ') from ' + @TargetTable +' where '+@PKField+'=@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 + '%'', ' + @TargetField + ') from ' + @TargetTable +' where '+@PKField+'=@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
GO

这篇日志没有评论。
此日志不可发表评论。