SQLPLUS结果集 - 输出到文件并插入表中

时间:2015-11-06 19:39:39

标签: sql oracle shell sqlplus spool

我需要通过SQLPLUS将select查询的结果输出到一个文件中,然后将这些记录插入到"发送的"表。虽然我无法弄清楚如何重新使用该结果存储在"发送的"表。我必须使用光标吗?我不太熟悉游标,但我认为可能是解决方案?还有另外一种方法吗?顺便说一句,这是一个shell脚本。

这是我到目前为止所做的:

sqlplus -S <<ENDOFSQL  || eval 'echo "Error occurred during sqlplus command line $LINENO" 1>&2; exit 1'
$USER/$PW@//$IP_PORT/$DB
SET PAGESIZE 50000
set feedback off
SET MARKUP HTML ON SPOOL ON
SET NUM 24
SPOOL REPORT.xls

select * from SOMETABLE A WHERE not exists (select 1 from SENT_TABLE B where   A.ID = B.ID);

SPOOL OFF
SET MARKUP HTML OFF SPOOL OFF

(NOW USE RESULTS FROM SPOOL AND INSERT INTO THE "SENT" TABLE) 

ENDOFSQL

1 个答案:

答案 0 :(得分:0)

如果您不想保留REPORT.xls的副本,请填写如下内容。但我建议你为此编写一个单独的SQL Loader脚本。

sqlplus -S <<ENDOFSQL  || eval 'echo "Error occurred during sqlplus command line $LINENO" 1>&2; exit 1'
$USER/$PW@//$IP_PORT/$DB
    SET PAGESIZE 50000
    set feedback off
    SET MARKUP HTML ON SPOOL ON
    SET NUM 24
    SPOOL report.sql

    select 'insert into SENT values('||column1||','||column2...||');' 
    from SOMETABLE A 
    WHERE not exists (select 1 
                        from SENT_TABLE B 
                        where   A.ID = B.ID);

    SPOOL OFF;
    @report.sql 
    COMMIT;
ENDOFSQL

以上情况与 -

一样好
INSERT INTO SENT
select *
    from SOMETABLE A 
    WHERE not exists (select 1 
                        from SENT_TABLE B 
                        where   A.ID = B.ID);