Oracle整表导出为update语句(存储过程实现)
项目中遇到的问题,需要把二区数据库中的一张表全量导出为sql文件,通过隔离传输软件同步到三区做入库操作。1 存储过程
存储过程实现如下,用到了 目录(dnl_tb_dir),序列(dnl_tbxl.nextval),游标等。create or replace procedure sp_rrtustat is id varchar2(20); val varchar2(4000); dnltb utl_file.file_type; cursor rrtustat_cursor is select "update RRTUSTAT set " || "SUCCTYPE = " || """" || succtype || """" || "," || "SUCCTIME = " || "to_date(" || """" || to_char(succtime, "yyyy-mm-dd hh24:mi:ss") || """" || "," || """yyyy-mm-dd hh24:mi:ss""" || ")," || "LASTTYPE = " || """" || lasttype || """" || "," || "LASTTIME = " || "to_date(" || """" || to_char(lasttime, "yyyy-mm-dd hh24:mi:ss") || """" || "," || """yyyy-mm-dd hh24:mi:ss""" || ")," || "LASTINFO = " || """" || lastinfo || """" || "," || "REMARK = " || """" || remark || """" || " where RTUID = " || rtuid || ";" sql from rrtustat; begin select (select to_char(sysdate, "yyyyMMddHH24miss") from dual) || lpad(dnl_tbxl.nextval, 3, "0") into id from dual; open rrtustat_cursor; dnltb := utl_file.fopen("DNL_TB_DIR","UPDATE-" || "RRTUSTAT-" || id || ".sql", "w"); loop fetch rrtustat_cursor into val; exit when rrtustat_cursor%notfound; utl_file.put_line(dnltb, val); end loop; close rrtustat_cursor; utl_file.put_line(dnltb, "commit;"); utl_file.put_line(dnltb, "exit;"); utl_file.fclose(dnltb); end;
2 Oracle job
job代码如下:begin sys.dbms_job.isubmit(job => 111, what => "SP_RRTUSTAT();", next_date => to_date("19-01-2022 11:53:59", "dd-mm-yyyy hh24:mi:ss"), interval => "sysdate + 5/1440"); commit; end; /