Linux

無法通過外殼(ksh)假離線

  • April 24, 2018

我編寫了一個 shell 腳本,在其中定義了變數:Last_Monthlast_date並且first_date我正在嘗試從 shell 執行以下 sql:

/app/oracle/product/10.2.0/bin/sqlplus -s user_name/passwd @effective_date.sql

內容effective_date.sql

set heading off
set feedback off
set verify off
set pagesize 50000
set trimspool on
set wrap off
set newpage none
set linesize 2000

spool /home/dir/file_${Last_Month};

select * from table_1
where effective_date between to_date("$first_date",'dd-mm-yyyy') and to_date("$last_date",'dd-mm-yyyy');

spool off;
exit;

但是在執行shell時,我遇到了以下錯誤:

SP2-0332: Cannot create spool file.
where effective_date between to_date("$first_date",'dd-mm-yyyy') and to_date("$last_date",'dd-mm-yyyy')
                                                                            *
ERROR at line 2:
ORA-00904: "$last_date": invalid identifier


not spooling currently

有什麼幫助嗎?

sqlplus您應該對 shell 變數使用不同的方法。你的命令應該變成:

/app/oracle/product/10.2.0/bin/sqlplus -s user_name/passwd @effective_date.sql "$Last_Month" "$first_date" "$last_date" 

你的 sql 腳本應該是:

set heading off
set feedback off
set verify off
set pagesize 50000
set trimspool on
set wrap off
set newpage none
set linesize 2000

spool /home/dir/file_'&1';

select * from table_1
where effective_date between to_date('&2','dd-mm-yyyy') and to_date('&3','dd-mm-yyyy');

spool off;
exit;

引用自:https://unix.stackexchange.com/questions/439639