oracle 使用spoo生成sql脚本

如果表table1和table2中id值相等,那么用table1中的value值更新table2中的value值。

下面是通过spool功能生成update语句脚本,可以创建*.sql脚本执行下面命令或者直接复制在command命令行执行。
-------------------------------------------------------------------------
set pagesize 0;     //输出每页行数,缺省为24,为了避免分页,可设定为0。
set feedback off; //回显本次sql命令处理的记录条数,缺省为on
set termout off; //显示脚本中的命令的执行结果,缺省为on
set linesize 600 //输出一行字符个数,缺省为80
col exportData for a1000;

select 'prompt version 1.0' from dual;
select 'SET DEFINE OFF' from dual;

drop table temp_sxh;
create table temp_sxh as with t1 as (
select id as id,
  value as value
    from table1 a
    where a.id in (select id from table2))
select a1, a2 from t1;

spool E:\work\temp_sxh.sql;       //生成脚本路径和文件名,该命令放在要生成得到的语句前
select 'update table2 a set a.value =' ||
   ''''|| b.value || ''''||
   'where a.id = '  ||
   ''''|| b.id ||''''|| ';' from temp_sxh b;

drop table temp_sxh;

 select 'commit;' from dual;
 spool off;
 set termout on; //去除标准输出每行的拖尾空格,缺省为off


--------------------------------------------------------------------
 此脚本执行后会生成temp_sxh.sql脚本文件,其中内容为:
 update table2 a set a.value ='100' where a.id = '100000';
 update table2 a set a.value ='101' where a.id = '100001';
 ....
 update table2 a set a.value ='101' where a.id = 'n00000';
 commit;

 如果没有关联信息,那么生成的temp_sxh.sql脚本文件,内容只有
 commit;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值