MSSQL批量替换Text字符串

本文提供了一个使用SQL游标和字符串函数更新数据库中特定URL的示例,展示了如何批量替换表内容中的旧链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

declare @old varchar(100)
declare @new varchar(100)
set @old='http://new.chaoren.com/CMS/localhost'
set @new='http://www.chaoren.com/CMS/localhost'
declare @ptr varbinary(16)
declare @newsid varchar(36)  --如为int则改为declare @newsid int
declare @Position int,@len int
set @len=datalength(@old)

declare yohen_Cursor scroll Cursor
for
select textptr([content]),[news_id] from t_news_content
where charindex(@old,content)>0 --and news_id='2007-06-08'
for read only

open yohen_Cursor
fetch next from yohen_Cursor into @ptr,@newsid
while @@fetch_status=0
begin
    select @Position=patindex('%' + @old + '%',[content]) from t_news_content where news_id=@newsid
    while @Position>0
    begin
    set @Position=@Position-1
    updatetext t_news_content.[content] @ptr @Position @len @new
    select @Position=patindex('%' + @old + '%',[content]) from t_news_content where news_id=@newsid
    end
    fetch next from yohen_Cursor into @ptr,@newsid
end
close yohen_Cursor
deallocate yohen_Cursor
go

转载于:https://www.cnblogs.com/hzuIT/articles/1417617.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值