Error while connecting the sqlplus from shell script - oracle

REQ_OUTPUT1=`echo $XXOLA_TOP/log/Inv/xxola_inv_item_conv_int`
echo Connecting in sqlplus
sqlplus -s $1#$TWO_TASK <<+ > $REQ_OUTPUT1
SET VERIFY OFF
WHENEVER SQLERROR EXIT FAILURE ROLLBACK;
SET LINESIZE 500;
SET SERVEROUTPUT ON SIZE 1000000;
SELECT 1 FROM DUAL;
exit;
Above is my code to connect sqlplus from shell program.
But when i am running this i am getting below error..
line 36: warning: here-document at line 29 delimited by end-of-file (wanted `+')

You are not ending the here document properly. Don't use symbols like + for here document delimiters. Use proper meaningful strings.
REQ_OUTPUT1=`echo $XXOLA_TOP/log/Inv/xxola_inv_item_conv_int`
echo Connecting in sqlplus
sqlplus -s $1#$TWO_TASK <<INP > $REQ_OUTPUT1
SET VERIFY OFF
WHENEVER SQLERROR EXIT FAILURE ROLLBACK;
SET LINESIZE 500;
SET SERVEROUTPUT ON SIZE 1000000;
SELECT 1 FROM DUAL;
exit;
INP

Related

Call PL/SQL block in unix script

I am trying to call pl/sql block in unix script using sqlplus. I just tried to print a statement but nothing is getting printed and I am not getting any error as well.
Result=`sqlplus -s $TgtUsrID/$TgtPswd#$TgtServer <<eof
whenever sqlerror exit sql.sqlcode;
SET SERVEROUTPUT ON;
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello World!');
END;
eof
`
current_time=`date`
echo " Script execution finished at $current_time"
Just need to prepend the variable(Result) with the dollar operator after the last EOF
along with an echo command such as
Result=`sqlplus -S /nolog << EOF
conn $TgtUsrID/$TgtPswd#$TgtServer
whenever sqlerror exit sql.sqlcode
set feedback off
SET SERVEROUTPUT ON;
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello World!');
END;
/
EOF`
echo $Result
echo Script execution finished at $(date)
would yield such a result
Hello World!
Script execution finished at Tue Feb 14 00:15:51 +03 2021
where quotes for the description after the last echo is redundant, and prefer using connection with /nolog as being securer, since the password of the schema would be unveiled whenever ps -ef command issued in the current case.

error in sql plus in executing large string header

I am getting this error in sql plus after executing this script . This is header of a file with >150 columns.I tried other options like
rtrim (xmlagg (xmlelement (e, x || '|')).extract ('//text()'), '|')
but no luck.
SP2-0027: Input is too long (> 2499 characters) - line ignored
abc.sh
sqlplus -s /nolog << EOF
connect abc/pwd#orcl;
whenever sqlerror exit sql.sqlcode;
set wrap ON
set trimout ON
set trimspool on
set serveroutput on
set pagesize 0
set long 20000000
set longchunksize 20000000
set linesize 4000
spool abc.sql
select TO_CLOB('SCSTSUMID|IMAILSUMID|FTOMERSUMKY|FGHERNAME|EILSUMADDR|FSTSUMNM|MIHZDLESUMNM|LZSTSUMNM|PRIMSUMADDR|SECSUMADDR|CITY|STATE|POSTALSUMCD|ISOSUMCODE|UNIVSUMDCOM|UNIVSUMDCOMSUMALWSUM13T17|UNIVSUMDCOMSUMALWSUMCAN|UNIVSUMDCOMSUMALWSUMCANSUM13T17|UNIVSUMPRTR|UNIVSUMPRTRSUMALWSUM13T17|UNIVSUMPRTRSUMALWSUMCAN|UNIVSUMPRTRSUMALWSUMCANSUM13T17|UNIVSUMSHOP|UNIVSUMDCOMSUMALWSUMUKDB|UNIVSUMLIVESUMCOMBINE|UNIVSUMLIVESUMDRAFT|UNIVSUMLIVESUMKICKOFF|UNIVSUMLIVESUMISGUKSUMUKDB|UNIVSUMLIVESUMISGUKSUMNOSUMUKDB|UNIVSUMLIVESUMISGMX|UNIVSUMLIVESUMPROBOWL|UNIVSUMLIVESUMSUPERBOWL|UNIVSUMDCOMSUMRECURR|UNIVSUMDCOMSUMRECURRSUM13T17|UNIVSUMDCOMSUMALWSUMHOLDOUT|UNIVSUMDCOMSUMRCRSUMALWSUMHLDOUT|AGE|GENDER|BIRTHSUMDT|CUSTSUMRECENCYSUMDT|HISPANICSUMFLG|FAVSUMTEAMSUMCD|ACTIVESUM0SUM3MSUMFLG|ACTIVESUM4SUM6MSUMFLG|ACTIVESUM7SUM9MSUMFLG|ACTIVESUM10SUM12MSUMFLG|ACTIVESUM0SUM12M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LUSTERSUMPNL|LSTSUMPBSUMFMPSUMSSN|EXTRACTSUMSEQ|HASDKSUMPROBS|SBSPNDSUMPROBS|DRAFTEDSUMFLG|EMAILSUMTIER|TSPSUMZIPSUMCD|TSPSUMSTATESUMABBREVIATION|TSPSUMADDRSUMTYPE|TSPSUMADVSUMHMSUMOWNR|TSPSUMADVSUMHHSUMSIZE|TSPSUMADVSUMHHSUMSIZESUMINDICATOR|TSPSUMADVSUMHHSUMMARITALSUMSTATUS|TSPSUMADVSUMHHSUMMARITALSUMSTATSUMIND|TSPSUMADVSUMDWELLINGSUMTYPE|TSPSUMADVSUMDWELLINGSUMTYPESUMIND|TSPSUMADVSUMTARGETSUMINCOMESUM2|TSPSUMADVSUMTARGETSUMINCOMESUMINDSUM2|TSPSUMADVSUMHHSUMEDUCATION|TSPSUMADVSUMHHSUMEDUCATIONSUMINDICATOR|TSPSUMAGESUMUNK|TSPSUMAGESUM75SUMPLUS|TSPSUMAGESUM65SUM74|TSPSUMAGESUM55SUM64|TSPSUMAGESUM45SUM54|TSPSUMAGESUM35SUM44|TSPSUMAGESUM25SUM34|TSPSUMAGESUM18SUM24|TSPSUMCHILDRENSUMAGESUM0SUM2|TSPSUMCHILDRENSUMAGESUM3SUM5|TSPSUMCHILDRENSUMAGESUM6SUM10|TSPSUMCHILDRENSUMAGESUM11SUM15|TSPSUMCHILDRENSUMAGESUM16SUM17|TSPSUMHHSUMTYPESUMFAMILYSUMCOMP|TSPSUMOCCUPATION|TSPSUMGENDERSUMPRSN|TSPSUMETHNICSUMGRP|TSPSUMETHNICSUMGRPSUMPRSN|TSPSUMLANGUAGESUMPRSN|TSPSUMTARGETSUMVALUESCORESUM2|TSPSUMCCSUMAMERSUMEXPRESS|TSPSUMCCSUMANYSUMCC|TSPSUMCCSUMBANKSUMCARD|TSPSUMCCSUMCATALOGSUMSHOWROOM|TSPSUMCCSUMCOMPUTERSUMELECTRONIC|TSPSUMCCSUMDEBITSUMCARD|TSPSUMCCSUMFINANCESUMCOSUMCARD|TSPSUMCCSUMFURNITURE|TSPSUMCCSUMGROCERY|TSPSUMCCSUMHMSUMIMPROVEMENT|TSPSUMCCSUMHMSUMOFFICESUMSUPPLY|TSPSUMCCSUMLOWSUMENDSUMDEPTSUMSTORE|TSPSUMCCSUMMAINSUMSTREETSUMRETAIL|TSPSUMCCSUMMASTERCARD|TSPSUMCCSUMMBRSHPSUMWHS|TSPSUMCCSUMMISC|TSPSUMCCSUMOILSUMGASSUMCARD|TSPSUMCCSUMSPECIALTYSUMAPPAREL|TSPSUMCCSUMSPORTINGSUMGOODS|TSPSUMCCSUMSTANDARDSUMRETAIL|TSPSUMCCSUMSTANDARDSUMSPECIALTYSUMCARD|TSPSUMCCSUMTRAVELSUMENTERTAINMENT|TSPSUMCCSUMTVSUMMAILSUMORDER|')||'TSPSUMCCSUMUPSCALESUMRETAIL|TSPSUMCCSUMUPSCALESUMSPECSUMRETAIL|TSPSUMCCSUMVISA|TSPSUMMTSUMFOOTBALLSUMENTHUSIASTS|TSPSUMMTSUMSMARTSUMPHONESUMUSER|TSPSUMMTSUMHIGHENDSUMAMSUMRANKSUMTM2|FANTSUMOPPSUMTEAMSUMNMSUMTM2|FANTSUMOPPSUMTEAMSUMRANKSUMTM2|FANTSUMLEAGUESUMIDSUMTM3|FANTSUMTEAMSUMIDSUMTM3|FANTSUMTEAMSUMNMSUMTM3|FANTSUMTEAMSUMRANKSUMTM3|FANTSUMOPPSUMTEAMSUMNMSUMTM3|FANTSUMOPPSUMTEAMSUMRANKSUMTM3|JEBBITSUMATTRIB13SUMFLG|JEBBITSUMATTRIB12SUMFANTSUMFLG|JEBBITSUMATTRIB11SUMKIDSSUMFLG|MSSUMONSUMLOCATION|MSSUMBARCLAY|BARCLAYSUMPASSIONSUMJETSETSUMIND|BARCLAYSUMPASSIONSUMELECSUMIND|BARCLAYSUMPASSIONSUMEGOERSUMIND|LSTSUMGPDSUMSTREAMSUMDT|GPDSUMMINSUMWATCHEDSUMCURRSUMSSN|GPDSUMMINSUMWATCHEDSUMPREV1SUMSSN|GPDSUMMINSUMWATCHEDSUMPREV2SUMSSN|FAVSUMTEAMSUMCDSUMIMPLIED|FAVSUMTEAMSUMCDSUMSUPPLIED|EXTRACTSUMDT' x from dual ;
spool off;
exit;
EOF
any suggestions will be highly appreciated.
You are hitting a physical limit on the length of a line of SQL in SQL*Plus. See this posting for more information on how to resolve: https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:4789399600346029472
Basically you're going to have to break up your input text with line breaks and concatenate the individual lines, like this:
abc.sh
sqlplus -s /nolog << EOF
connect abc/pwd#orcl;
whenever sqlerror exit sql.sqlcode;
set wrap ON
set trimout ON
set trimspool on
set serveroutput on
set pagesize 0
set long 20000000
set longchunksize 20000000
set linesize 4000
spool abc.sql
select TO_CLOB('SCSTSUMID|IMAILSUMID|FTOMERSUMKY|FGHERNAME|EILSUMADDR|FSTSUMNM|MIHZDLESUMNM|LZSTSUMNM|PRIMSUMADDR|SECSUMADDR|CITY|STATE|POSTALSUMCD|ISOSUMCODE|UNIVSUMDCOM|UNIVSUMDCOMSUMALWSUM13T17|UNIVSUMDCOMSUMALWSUMCAN|UNIVSUMDCOMSUMALWSUMCANSUM13T17|UNIVSUMPRTR|UNIVSUMPRTRSUMALWSUM13T17|UNIVSUMPRTRSUMALWSUMCAN|UNIVSUMPRTRSUMALWSUMCANSUM13T17|UNIVSUMSHOP|UNIVSUMDCOMSUMALWSUMUKDB|UNIVSUMLIVESUMCOMBINE|UNIVSUMLIVESUMDRAFT|UNIVSUMLIVESUMKICKOFF|UNIVSUMLIVESUMISGUKSUMUKDB|'
|| 'UNIVSUMLIVESUMISGUKSUMNOSUMUKDB|UNIVSUMLIVESUMISGMX|UNIVSUMLIVESUMPROBOWL|UNIVSUMLIVESUMSUPERBOWL|UNIVSUMDCOMSUMRECURR|UNIVSUMDCOMSUMRECURRSUM13T17|UNIVSUMDCOMSUMALWSUMHOLDOUT|UNIVSUMDCOMSUMRCRSU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MLSTSUMACTIVITYSUMDT|NFLNSUMAAMSUMSEGMENTSUMFLG|CURRSUMSSNSUMVIDEOSUMVIEWSSUMFLG|'
|| 'GPDSUMSTATUS|GPISUMSTATUS|GPESUMSTATUS|MXSUMOPTSUMINSUMFLAG|DESUMOPTSUMINSUMFLAG|CASUMOPTSUMINSUMFLAG|UKSUMOPTSUMINSUMFLAG|CLUSTERSUMN|CLUSTERSUMPNL|LSTSUMPBSUMFMPSUMSSN|EXTRACTSUMSEQ|HASDKSUMPROBS|SBSPNDSUMPROBS|DRAFTEDSUMFLG|EMAILSUMTIER|TSPSUMZIPSUMCD|TSPSUMSTATESUMABBREVIATION|TSPSUMADDRSUMTYPE|TSPSUMADVSUMHMSUMOWNR|TSPSUMADVSUMHHSUMSIZE|TSPSUMADVSUMHHSUMSIZESUMINDICATOR|TSPSUMADVSUMHHSUMMARITALSUMSTATUS|TSPSUMADVSUMHHSUMMARITALSUMSTATSUMIND|TSPSUMADVSUMDWELLINGSUMTYPE|'
|| 'TSPSUMADVSUMDWELLINGSUMTYPESUMIND|TSPSUMADVSUMTARGETSUMINCOMESUM2|TSPSUMADVSUMTARGETSUMINCOMESUMINDSUM2|TSPSUMADVSUMHHSUMEDUCATION|TSPSUMADVSUMHHSUMEDUCATIONSUMINDICATOR|TSPSUMAGESUMUNK|TSPSUMAGESUM75SUMPLUS|TSPSUMAGESUM65SUM74|TSPSUMAGESUM55SUM64|TSPSUMAGESUM45SUM54|TSPSUMAGESUM35SUM44|TSPSUMAGESUM25SUM34|TSPSUMAGESUM18SUM24|TSPSUMCHILDRENSUMAGESUM0SUM2|TSPSUMCHILDRENSUMAGESUM3SUM5|TSPSUMCHILDRENSUMAGESUM6SUM10|TSPSUMCHILDRENSUMAGESUM11SUM15|TSPSUMCHILDRENSUMAGESUM16SUM17|'
|| 'TSPSUMHHSUMTYPESUMFAMILYSUMCOMP|TSPSUMOCCUPATION|TSPSUMGENDERSUMPRSN|TSPSUMETHNICSUMGRP|TSPSUMETHNICSUMGRPSUMPRSN|TSPSUMLANGUAGESUMPRSN|TSPSUMTARGETSUMVALUESCORESUM2|TSPSUMCCSUMAMERSUMEXPRESS|TSPSUMCCSUMANYSUMCC|TSPSUMCCSUMBANKSUMCARD|TSPSUMCCSUMCATALOGSUMSHOWROOM|TSPSUMCCSUMCOMPUTERSUMELECTRONIC|TSPSUMCCSUMDEBITSUMCARD|TSPSUMCCSUMFINANCESUMCOSUMCARD|TSPSUMCCSUMFURNITURE|TSPSUMCCSUMGROCERY|TSPSUMCCSUMHMSUMIMPROVEMENT|TSPSUMCCSUMHMSUMOFFICESUMSUPPLY|TSPSUMCCSUMLOWSUMENDSUMDEPTSUMSTORE|'
|| 'TSPSUMCCSUMMAINSUMSTREETSUMRETAIL|TSPSUMCCSUMMASTERCARD|TSPSUMCCSUMMBRSHPSUMWHS|TSPSUMCCSUMMISC|TSPSUMCCSUMOILSUMGASSUMCARD|TSPSUMCCSUMSPECIALTYSUMAPPAREL|TSPSUMCCSUMSPORTINGSUMGOODS|TSPSUMCCSUMSTANDARDSUMRETAIL|TSPSUMCCSUMSTANDARDSUMSPECIALTYSUMCARD|TSPSUMCCSUMTRAVELSUMENTERTAINMENT|TSPSUMCCSUMTVSUMMAILSUMORDER|')||'TSPSUMCCSUMUPSCALESUMRETAIL|TSPSUMCCSUMUPSCALESUMSPECSUMRETAIL|TSPSUMCCSUMVISA|TSPSUMMTSUMFOOTBALLSUMENTHUSIASTS|TSPSUMMTSUMSMARTSUMPHONESUMUSER|'
|| 'TSPSUMMTSUMHIGHENDSUMAMSUMRANKSUMTM2|FANTSUMOPPSUMTEAMSUMNMSUMTM2|FANTSUMOPPSUMTEAMSUMRANKSUMTM2|FANTSUMLEAGUESUMIDSUMTM3|FANTSUMTEAMSUMIDSUMTM3|FANTSUMTEAMSUMNMSUMTM3|FANTSUMTEAMSUMRANKSUMTM3|FANTSUMOPPSUMTEAMSUMNMSUMTM3|FANTSUMOPPSUMTEAMSUMRANKSUMTM3|JEBBITSUMATTRIB13SUMFLG|JEBBITSUMATTRIB12SUMFANTSUMFLG|JEBBITSUMATTRIB11SUMKIDSSUMFLG|MSSUMONSUMLOCATION|MSSUMBARCLAY|BARCLAYSUMPASSIONSUMJETSETSUMIND|BARCLAYSUMPASSIONSUMELECSUMIND|BARCLAYSUMPASSIONSUMEGOERSUMIND|LSTSUMGPDSUMSTREAMSUMDT|'
|| 'GPDSUMMINSUMWATCHEDSUMCURRSUMSSN|GPDSUMMINSUMWATCHEDSUMPREV1SUMSSN|GPDSUMMINSUMWATCHEDSUMPREV2SUMSSN|FAVSUMTEAMSUMCDSUMIMPLIED|FAVSUMTEAMSUMCDSUMSUPPLIED|EXTRACTSUMDT' x
from dual ;
spool off;
exit;
EOF

How to capture the output of a PL SQL function in a powershell variable?

I am able to connecting to remote oracle database using powershell and sqlplus. Also I am able to capture the output of the command/query executed in the database into a powershell variable.
PFB example.
$username ="hr"
$password ="hr"
$tnsalias ="orcl"
$outputfilepath="C:\Users\Desktop"
$sqlquery=#"
set serveroutput off
set feedback off
set heading off
set echo off
select sysdate from dual;
"#
Running the above query in oracle database as below
$xval=$sqlquery | sqlplus -silent $username/$password#$tnsalias
echo $xval
The output will be
23-APR-18
The problem is when I am running a PL SQL function as below.
$sqlquery=#"
set serveroutput off
set feedback off
set heading off
set echo off
declare
x varchar2:=null;
exec x:=MYTEST_FUN('a','b');
"#
There is no value captured in the $xval variable and also the function is not running.
How to execute the function such that the return value of the function is captured in a powershell variable.
One way is to make it set serveroutput on and use DBMS_OUTPUT.PUT_LINE() within BEGIN..END
Another option is to use the sqlplus' PRINT command.
set serveroutput off
set feedback off
set heading off
set echo off
variable x VARCHAR2 --declare bind variable x
exec :x := MYTEST_FUN('a','b'); --a colon before x needed.
PRINT x;
Worked for me:
$sqlQuery = #"
set serveroutput off
set feedback off
set heading off
set echo off
variable x NUMBER;
select count(*) into :x from TABLE;
PRINT x;
exit
"#
($sqlQuery | sqlplus -S user/pass | Write-Output | Out-String).Trim()
Late entry, most concisely:
$dateVar = ("set heading off
select sysdate from dual;" | sqlplus -s $yourConnectionString | Out-String).Trim()

Error Handling of SQLPlus from Bash - not working

I have read every relevant link on this issue and similar questions/answers, preveland answer is to first set whenever SQLERROR EXIT SQL.SQLCODE;
Only then do the query, and then inspect the SQL Plus return code using: ERRORCODE=$?
Here is a sample script:
GetAmountOfChunks()
{
export CHUNK_AMOUNT=`sqlplus -s $CONSTR<<SQL
set heading off;
set trim on;
set feed off;
whenever SQLERROR EXIT SQL.SQLCODE;
select 1/0 from dual;
--SELECT COUNT(*) FROM CNV_CHUNKS_PROC_STATUS;
/
SQL`
When ran it debug mode, it gives:
++ sqlplus -s USER/PASS#HOST/DB
+ export 'CHUNK_AMOUNT= select 1/0 from dual
*
ERROR at line 1:
ORA-01476: divisor is equal to zero'
+ CHUNK_AMOUNT=' select 1/0 from dual
*
ERROR at line 1:
ORA-01476: divisor is equal to zero'
+ ERRORCODE=0
+ '[' 0 -ne 0 ']'
As you can see, returned code is 0!
I expected if not 1476, then at least 196 (right most 8 bytes), but not 0 which indicates success!
Please help...
Thanks.
Your ERRORCODE is being set to zero because that's the exit code from the subshell you're running SQL*Plus is, viq the backticks. The exit code of the SQL*Plus process is 196 but you aren't capturing that, and it's not that easy to do that within a heredoc. The stdout from the process - which is what you are capturing - is not that exit code, it's the query and error message being printed. And even if you could capture it, I'm not sure how you'd distinguish between the 196 coming from an error, or from your actual query.
You could do something like running the query in a block that hides the error and prints either a default value or the actual calculated value, or only look at the last line of output and try to interpret that; but you'll still be fighting against it showing the command being run. SQL*Plus has set echo off but that doesn't do anything with an interactive session, which this still is with input redirection.
Another way to go is to create a script file and temporarily store the output:
echo "
set pages 0
set trim on
set feed off
set echo off
whenever SQLERROR EXIT FAILURE
select 1/0 from dual;
--SELECT COUNT(*) FROM CNV_CHUNKS_PROC_STATUS;
exit 0;
" > /tmp/GetAmountOfChunks_$$.sql
sqlplus -s -l $CONSTR #/tmp/GetAmountOfChunks_$$.sql > /tmp/GetAmountOfChunks_$$.out
if [[ $? -eq 0 ]]; then
export CHUNK_AMOUNT=`cat /tmp/GetAmountOfChunks_$$.out`
else
# whatever you want to do on error; show output file? set default?
cat /tmp/GetAmountOfChunks_$$.out
fi
rm -f /tmp/GetAmountOfChunks_$$.sql /tmp/GetAmountOfChunks_$$.out
This creates a (process-specific) .sql file; executes that write the output (minus the statement, via set echo off) to a .out file; checks the SQL*Plus exit code; and if that is zero gets the result from the file.
As you hinted relying on SQL.SQLCODE to detect an error from your shell script is dangerous as you could get an error that wraps to zero, so I've used the generic FAILURE. If you need the real error code you can get it from the output file.
Another approach using a PL/SQL block:
set -f
CHUNK_AMOUNT=`sqlplus -s $CONSTR <<SQL
set heading off;
set trim on;
set feed off;
whenever SQLERROR EXIT FAILURE;
set serveroutput on;
declare
chunk_amount number;
begin
select 1/0 into chunk_amount from dual;
--SELECT COUNT(*) INTO chunk_amount FROM CNV_CHUNKS_PROC_STATUS;
dbms_output.put_line(chunk_amount);
exception
when others then
dbms_output.put_line(sqlcode);
end;
/
exit 0
SQL
exit $?`
ERRORCODE=$?
If the PL/SQL block runs then ERRORCODE will be zero and CHUNK_AMOUNT will be the calculated value if it's successful, or the SQL code if it throws an exception; since that will be negative (-1476 in your example) you can test that to see if it's expected, if you're only expecting positive values.
If the block can't run because of a syntax error or invalid credentials (notice the -l flag I snuck in) then ERRORCODE will be 1 and CHUNK_AMOUNT will have the error text, e.g. ERROR: ORA-12154: TNS:could not resolve the connect identifier... or whatever actually went wrong. The set -f stops a * in the error message being expanded into a file list from the current directory.
Or even more simply and closer to your original:
set -f
CHUNK_AMOUNT=`sqlplus -s $CONSTR <<SQL
set heading off;
set trim on;
set feed off;
whenever SQLERROR EXIT FAILURE;
select 1/0 from dual;
--SELECT COUNT(*) FROM CNV_CHUNKS_PROC_STATUS;
exit 0
SQL
exit $?`
ERRORCODE=$?
and now ERRORCODE is 0 on success and CHUNK_AMOUNT has the calculated value on any error ERRORCODE is 1, you can test that directly, and the actual error is always in CHUNK_AMOUNT - but only as a string, you don't get -1476 this way.

call a procedure with date parameter in sqlplus oracle using shell script

I want to call a procedure in sqlplus oracle using shell script,
my procedure name is getdate_proc with two parameters, startdate and enddate.
I want to set startdate = sysdate and enddate = sysdate + 5 days
for example :execute getdate_proc(to_date('05/05/2015', 'MM-DD-YYYY'),to_date('05/09/2015','MM-DD-YYYY'))
below is my code:
#!/usr/bin/ksh
sqlplus -s /nolog << EOF
connect scott/tiger
--execute procedure with parameter
execute getdate_proc(to_date(sysdate, 'MM-DD-YYYY'),to_date(sysdate + 5days,'MM-DD-YYYY'))
--set spooling to save in csv
set underline off
SET RECSEP OFF
set verify off
set colsep ','
set linesize 300
set trimspool on
spool /home/user/project/samp.csv
select * from att2;
spool off
set verify off
i solved my problem with this code:
#!/usr/bin/ksh
date1='date'
date1=$(/bin/date --date="$date1" -d "+0 day" +"%F")
date2=$(/bin/date --date="$date1" -d "+4 day" +"%F")
echo $date1
echo $date2
sqlplus -s /nolog << EOF
connect scott/tiger
--execute procedure with parameter
execute attendance(to_date('$date1', 'YYYY-MM-DD'),to_date('$date2','YYYY-MM-DD'))
--set spooling to save in csv
set underline off
SET RECSEP OFF
set verify off
set colsep ','
set linesize 300
set trimspool on
spool /home/user/project/samp.csv
select * from att2;
spool off
set verify off
:) cheers

Resources