你的位置: Kiyo'Space首页 数据库 阅读文章 欢迎留下您的足迹

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 ‘代码’,'替换内容’

没有评论

  • (Required)
  • (Required, will not be published)