回 帖 发 新 帖 刷新版面

主题:一个至今无法解决的蛋痛问题

/*

我已经快疯了,数据库总过200张表左右,每张表大概有20几个字段,一张表有几百万的数据,
下面的存储过程执行下去,4小时没走完,我还要替换这样的数值大概20个,oh my god 谁能帮助我....



从全个数据库中,进行全文替换的存储过程 遍历整个数据库,对全部表的字段进行文本替换

调用语句在最后

*/
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 Replace_tablename '欲替换的原值','新值'




*/

回复列表 (共1个回复)

沙发


qq:1624568175 请高手解决了,联系我

我来回复

您尚未登录,请登录后再回复。点此登录或注册