从全个数据库中,进行全文替换的存储过程

遍历整个数据库,对全部表的字段进行文本替换

CREATE PROCEDURE Replace_tablename
@strword varchar(200)
,@replaceword varchar(200)
AS
BEGIN
declare @count int
,@ziduanname varchar(50)
,@tablename varchar(50)
,@ziduanleixing varchar(50)
,@sql Nvarchar(800)
declare cur_LocationList cursor fast_forward for
select a.name ,b.name,c.name from syscolumns a
inner join sysobjects b
on a.id=b.id and b.xtype='u' and lower(a.name) not in('desc','order','top','left','bottom','right')
inner join systypes c
on a.xtype=c.xusertype
and c.name in('varchar','char','nvarchar','nchar')
open cur_LocationList
fetch next from cur_LocationList into @ziduanname,@tablename,@ziduanleixing
while @@fetch_status = 0
begin
set @count=0
    set @sql='update ['+@tablename+'] set [' + @ziduanname +']=replace(['+ @ziduanname +'],''' + @strword +''','''+@replaceword +''')      where ['+@ziduanname+'] like ''%'+@strword+'%'''
print '表:'+@tablename+'    列:' + @ziduanname
exec sp_executeSql @sql
fetch next from cur_LocationList into @ziduanname,@tablename,@ziduanleixing
end
close cur_LocationList
deallocate cur_LocationList
END
GO

使用如下:

exec exec Replace_tablename '欲替换的原值','新值'

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值