替换数据库里面所有表的数据

wljie1010 2008-06-26 08:07:23
替换数据库里面所有用户的表的数据
把表中的每条记录每个字段的包含 ‘MMCP009’ 替换成 ‘MMCP001’

所有表,所有记录,所有字段
...全文
125 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
nzperfect 2008-06-26
  • 打赏
  • 举报
回复
哎,我1楼的就是注入的反写,我写给别人都用过很多次了 ~
wzy_love_sly 2008-06-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wljie1010 的回复:]
楼上不对,一楼的我试一下
[/Quote]

楼主是2005吧,用游标
hery2002 2008-06-26
  • 打赏
  • 举报
回复
使用替换注入的那个脚本就可以了,
如楼上
中国风 2008-06-26
  • 打赏
  • 举报
回复
加上条件 'where '+ quotename(b.Name)+' like ''%MMCP009%'''

--用游标
declare T cursor local for
select
'update '+quotename(a.Name)+' set '+quotename(b.Name)+'=replace('+quotename(b.Name)+',''MMCP009'',''MMCP001'') where '+
quotename(b.Name)+' like ''%MMCP009%'''
from
sysobjects a
join
syscolumns b on a.ID=b.ID
join
systypes c on c.xusertype=b.xtype
where
a.xtype='U' and c.Name in('nvarchar','char')--定义类型
declare @s nvarchar(1000)
open t
fetch next from t into @s
while @@fetch_status=0
begin
exec @s
fetch next from t into @s
end
close t
deallocate t
中国风 2008-06-26
  • 打赏
  • 举报
回复

select
'update '+quotename(a.Name)+' set '+quotename(b.Name)+'=replace('+quotename(b.Name)+',''MMCP009'',''MMCP001'') where '+
quotename(b.Name)+' like ''%MMCP009%'''
from
sysobjects a
join
syscolumns b on a.ID=b.ID
join
systypes c on c.xusertype=b.xtype
where
a.xtype='U' and c.Name in('nvarchar','char')--定义类型

用以上生成语句,在查询分析器里执行
wljie1010 2008-06-26
  • 打赏
  • 举报
回复
楼上不对,一楼的我试一下
wzy_love_sly 2008-06-26
  • 打赏
  • 举报
回复
DECLARE hCForEach CURSOR GLOBAL
FOR
SELECT N'update '+QUOTENAME(o.name)
+N' set '+ QUOTENAME(c.name) + N' = replace(' + QUOTENAME(c.name) + ',''MMCP009'',''MMCP001'')'
FROM sysobjects o,syscolumns c,systypes t
WHERE o.id=c.id
AND OBJECTPROPERTY(o.id,N'IsUserTable')=1
AND c.xusertype=t.xusertype
AND t.name='varchar' or t.name='nvarchar'--自己加
EXEC sp_MSforeach_Worker @command1=N'?'
中国风 2008-06-26
  • 打赏
  • 举报
回复
--2000用sp_MSforeach_worker
中国风 2008-06-26
  • 打赏
  • 举报
回复
update t
set col='MMCP001'
where
col='MMCP009'
nzperfect 2008-06-26
  • 打赏
  • 举报
回复
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='MMCP009'
set @str2='MMCP001'
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;

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧