Sql批量替换所有表中内容
创建过程-----
CREATE PROCEDURE RpScript
@str varchar(500),
@str2 varchar(500)
AS
BEGIN
/*
select a.id as ID,a.name as Tab_Name,b.name as Cloumn_Name,c.name as Cloumn_Type
from sysobjects a,syscolumns b ,systypes c
where a.id=b.id and b.xtype=c.xtype and a.xtype='u' and c.name
in ('char', 'nchar', 'nvarchar', 'varchar','text','ntext')
*/
declare @t varchar(255),@c varchar(255)
declare table_cursor cursor for
select a.name,b.name from sysobjects a,syscolumns b ,systypes c
where a.id=b.id and b.xtype=c.xtype and a.xtype='u' and c.name in ('char', 'nchar', 'nvarchar', 'varchar','text','ntext')/* –这里如果你的text(ntext)类型没有超过8000(4000)长度,才可以使用*/
open table_cursor
fetch next from table_cursor into @t,@c
while(@@fetch_status=0)
begin
print('update [' + @t + '] set [' + @c + ']=replace(cast([' + @c + '] as varchar(8000)),'''+@str+''','''+ @str2 +''')');
exec('update [' + @t + '] set [' + @c + ']=replace(cast([' + @c + '] as varchar(8000)),'''+@str+''','''+ @str2 +''')');
fetch next from table_cursor into @t,@c
end
close table_cursor
deallocate table_cursor;
END
GO
-----
用的时候 exec RpScript '代码','替换内容'
版权声明:
作者:Kiyo
链接:https://www.wkiyo.cn/html/2011-11/i753.html
来源:Kiyo's space
文章版权归作者所有,未经允许请勿转载。
共有 0 条评论