求一个存储过程 将库里所有表中指定的字符批量替换成别的字符

gundamff 2009-05-07 11:07:44
RT 谢谢了先
...全文
170 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
gundamff 2009-05-07
  • 打赏
  • 举报
回复
呃.....ls能给个例子不
mosaic 2009-05-07
  • 打赏
  • 举报
回复
在对表的字段再加一层循环逐个字段进行更新呗。

可以从user_table_columns里面根据表名获取各个字段名及其数据类型,可以判断一下,只对字符型的字段进行更新。
gundamff 2009-05-07
  • 打赏
  • 举报
回复
如果表的字段也不指定那...
hongqi162 2009-05-07
  • 打赏
  • 举报
回复

写了一个你参考一下吧

declare
cursor tb is select table_name from user_all_tables ;
table_name varchar2(100);
updatestr varchar2(1000);
colname varchar2(100);
colvalue varchar2(100);
begin
colname:='col1';
open tb;
loop
fetch tb into table_name;
exit when tb%notfound;
updatestr:= 'update '||table_name||' set col1='''||colvalue||'''';
--execute immediate updatestr; --执行update语句
dbms_output.put_line(updatestr);
end loop;
close tb;
end;
mosaic 2009-05-07
  • 打赏
  • 举报
回复
晕倒,编辑有问题,重发:



declare
cursor tb is select table_name from user_all_tables ;
type cursor_t is ref cursor;
tableinfo cursor_t;

table_name user_tab_columns.TABLE_NAME%type;
updatestr varchar2(4000);
column_name user_tab_columns.COLUMN_NAME%type;
data_type user_tab_columns.data_type%type;
oldstr varchar2(10):='源字符串'; --根据实际情况确定内容和变量长度
newstr varchar2(10):='目标字符串'; --根据实际情况确定内容和变量长度

begin

open tb;
loop
fetch tb into table_name;
exit when tb%notfound;
open tableinfo for 'select column_name,data_type from user_tab_columns where table_name=:tabname' using table_name;
loop
fetch tableinfo into column_name,data_type;
exit when tableinfo%notfound;

if data_type='VARCHAR2' or data_type='VARCAHR' or data_type='CHAR' then ---可根据需要确定哪些数据类型

updatestr:= 'update '||table_name||' set '||column_name||'= replace('||column_name||','''||oldstr||''','''||newstr||''')';
-- execute immediate updatestr; --确定拼装的SQL无误后可以打开执行update语句
dbms_output.put_line(updatestr); ---可能会报缓冲区满,没有关系,只要看一些前面的update语句是否正确。正式执行时注释掉
end if;
end loop;
close tableinfo;

end loop;
close tb;
end;
mosaic 2009-05-07
  • 打赏
  • 举报
回复
供参考:

declare
cursor tb is select table_name from user_all_tables ;
type cursor_t is ref cursor;
tableinfo cursor_t;
--cursor tableinfo(tabname) is select column_name,data_type from user_tab_columns where table_name=tabname;
table_name user_tab_columns.TABLE_NAME%type;
updatestr varchar2(4000);
column_name user_tab_columns.COLUMN_NAME%type;
data_type user_tab_columns.data_type%type;
oldstr varchar2(10):='源字符串'; --根据实际情况确定内容和变量长度
newstr varchar2(10):='目标字符串'; --根据实际情况确定内容和变量长度begin

open tb;
loop
fetch tb into table_name;
exit when tb%notfound;
open tableinfo for 'select column_name,data_type from user_tab_columns where table_name=:tabname' using table_name;
loop
fetch tableinfo into column_name,data_type;
exit when tableinfo%notfound;

if data_type='VARCHAR2' or data_type='VARCAHR' or data_type='CHAR' then ---可根据需要确定哪些数据类型 updatestr:= 'update '||table_name||' set '||column_name||'= replace('||column_name||','''||oldstr||''','''||newstr||''')';
-- execute immediate updatestr; --确定拼装的SQL无误后可以打开执行update语句
dbms_output.put_line(updatestr); ---可能会报缓冲区满,没有关系,只要看一些前面的update语句是否正确。正式执行时注释掉 end if;
end loop;
close tableinfo;

end loop;
close tb;
end;
gundamff 2009-05-07
  • 打赏
  • 举报
回复
replaceAll('','','')
有这方法??????
wangchm168 2009-05-07
  • 打赏
  • 举报
回复
replaceAll('','','')

3,491

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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