批量替换SQL SERVER库中ntext字段的存储过程

SQL存储过程批量替换字符串
本文介绍了一个SQL Server存储过程的实现,该过程用于遍历数据库表中的所有记录,并批量替换指定字段内的字符串。通过定义两个不同的游标,分别完成两种不同的字符串替换任务。
USE [NewsPaper]
GO


/****** Object:  StoredProcedure [dbo].[ReplaceMyStr]    Script Date: 11/07/2013 02:03:50 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ReplaceMyStr]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ReplaceMyStr]
GO


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO




create  procedure [dbo].[ReplaceMyStr]
as


declare @ID int
declare @strtmp varchar(max)
declare @strsrc char(25),@strdsc char(10)
declare @strsrc2 char(12),@strdsc2 char(15)
set @strsrc = '<img&nbsp;src=&nbsp;../..'
set @strdsc = '<img src="'
set @strsrc2 = '.jpg></P><P>'
set @strdsc2 = '.jpg" /><P></P>'


declare replace_Cursor scroll Cursor
for 
select id from yao_article
--for read only
open replace_Cursor
fetch next from replace_Cursor into @ID
while @@fetch_status=0
begin
    select @strtmp = [Content] from yao_article where id=@ID
    select @strtmp = Replace(@strtmp,@strsrc,@strdsc)
    update yao_article set [Content] = @strtmp where id=@ID
       
    fetch next from replace_Cursor into @ID
end
close replace_Cursor
deallocate replace_Cursor


declare replace_Cursor2 scroll Cursor
for 
select id from yao_article
--for read only
open replace_Cursor2
fetch next from replace_Cursor2 into @ID
while @@fetch_status=0
begin
    
    select @strtmp = [Content] from yao_article where id=@ID
    select @strtmp = Replace(@strtmp,@strsrc2,@strdsc2)
    update yao_article set [Content] = @strtmp where id=@ID
        
    fetch next from replace_Cursor2 into @ID
end
close replace_Cursor2
deallocate replace_Cursor2
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值