简体   繁体   中英

Error while executing Merge through shell script

I am trying to execute a script which calls Merge statement through sqlplus.

The merge statement is executing fine but when I am executing it through shell script, it's giving an error :

current_time=`date`
echo "FSG_PRCB_PRVD_FAC_SEQ table UPDATE STARTED at $current_time"
Result=`sqlplus -s $TgtUsrID/$TgtPswd@$TgtServer <<eof
whenever sqlerror exit sql.sqlcode; 
merge into FSG_WRK.FSG_PRCB_FAC_REF_DATA T1
   using
    (select NUM_ID, ALPHA_NUM_ID from FSG.FSG_PRCB_ADDRESS_CROSSWLK) T2
  ON (T1.ADDR_TYPE=T2.ALPHA_NUM_ID)
when matched then
     update set T1.ADDR_TYPE_N = T2.NUM_ID ;
eof
`

ERRORCODE=$?

#Check the return code from SQL Plus
if [ $ERRORCODE != 0 ]
then
  echo "********************"
  echo "ERROR: Updating Status Failed. ErrorCode: $ERRORCODE"
  exit -1
else
  echo "********************"
  echo "UUpdating Status  returns $Result"
       current_time=`date`
       echo "Updating Status Query finished at $current_time"
fi

Output :

updating status failed.Errorcode:174

I am not sure what went wrong.

Using exit sql.sqlcode can be misleading, or even dangerous.

From The Linux Documentation Project :

Out of range exit values can result in unexpected exit codes. An exit value greater than 255 returns an exit code modulo 256. For example, exit 3809 gives an exit code of 225 (3809 % 256 = 225).

You're seeing the exit code reported as 174, but the original error code could have been anything where mod(<actual code>, 256) evaluates to 174. You'll be able to see the actual error code and message in your $Result variable.

A likely candidate is an ORA-00942 error (as mod(942, 256) is 174) - and as you've said the merge works standalone, you'll probably find that the user you're connecting to and running the merge as, represented by $TgtUsrID , does not have the necessary privileges on the tables owned by FSG and/or FSG_WRK .

This shows that the reported exit code can be confusing or misleading. The potentially dangerous part if if you get a real error code which is an exact multiple of 256, such as the plausible ORA-01536 "space quota exceeded for tablespace %s'. As mod(1536, 256) is zero, $ERRORCODE will be zero too, so your check:

if [ $ERRORCODE != 0 ]

will be false even though an error did occur.

It will probably be simpler to change your SQL to have whenever sqlerror exit failure (maybe with the addition of rollback ?), and then your check for non-zero will work; and you can use $Result to see/report what was actually wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM