方法一:
exec sp_msforeachtable @command1=N'
declare @s nvarchar(4000),@tbname sysname
select @s=N'''',@tbname=N''?''
select @s=@s+N'',''+quotename(a.name)+N''=replace(''+quotename(a.name)+N'',N''''aa'''',N''''bb'''')''
from syscolumns a,systypes b
where a.id=object_id(@tbname)
and a.xusertype=b.xusertype
and b.name like N''%char''
if @@rowcount>0
begin
set @s=stuff(@s,1,1,N'''')
exec(N''update ''+@tbname+'' set ''+@s)
end'
declare @s nvarchar(4000),@tbname sysname
select @s=N'''',@tbname=N''?''
select @s=@s+N'',''+quotename(a.name)+N''=replace(''+quotename(a.name)+N'',N''''aa'''',N''''bb'''')''
from syscolumns a,systypes b
where a.id=object_id(@tbname)
and a.xusertype=b.xusertype
and b.name like N''%char''
if @@rowcount>0
begin
set @s=stuff(@s,1,1,N'''')
exec(N''update ''+@tbname+'' set ''+@s)
end'
方法二:
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 a.xtype='u' and c.name in (--这里是要替换的类型
'char', 'nchar', 'nvarchar', 'varchar','text','ntext' --这里如果你的text(ntext)类型没有超过8000(4000)长度,才可以使用
)
declare @str varchar(500),@str2 varchar(500)
--这里是你要替换的原字符
set @str='aa'
--这里是你要替换的新字符
set @str2='bb'
open table_cursor fetch next from table_cursor into @t,@c
while(@@fetch_status=0)
begin
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;
declare table_cursor cursor for
select a.name,b.name from sysobjects a,syscolumns b ,systypes c
where a.id=b.id and a.xtype='u' and c.name in (--这里是要替换的类型
'char', 'nchar', 'nvarchar', 'varchar','text','ntext' --这里如果你的text(ntext)类型没有超过8000(4000)长度,才可以使用
)
declare @str varchar(500),@str2 varchar(500)
--这里是你要替换的原字符
set @str='aa'
--这里是你要替换的新字符
set @str2='bb'
open table_cursor fetch next from table_cursor into @t,@c
while(@@fetch_status=0)
begin
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;
作者:山边小溪
主站:yyyweb.com 记住啦:)
欢迎任何形式的转载,但请务必注明出处。