oracle - SQL*Plus in Shell Script: Send COUNT(*) to Variable, Error to File -


i'm calling oracle sql*plus korn shell (ksh).

my ultimate goal value sql statement's count(*) shell-script variable.

however, if sql error raised, report sql exception in full , terminate script.

i'm having hard time accounting both scenarios satisfaction.

#!/bin/ksh  test_count=$(sqlplus -s scott/tiger << eof1     whenever sqlerror exit 1     set pagesize 0 feedback off verify off heading off echo off     select count(*) scott.emp empno > 7777;     exit; eof1 )  sql_ret_code=$?  if [ $sql_ret_code -ne 0 ]     echo "error encountered."     echo     exit 1 fi  echo "test_count = $test_count"  exit 0 

in "happy path" case, above works (in displays test_count = 8).

if simulate sql error referencing nonexistent table (e.g., "scott.emp_bogus"), code above prints generic message "error encountered." prefer report full oracle exception, this:

error encountered.  select count(*) scott.emp_bogus empno > 7777                           * error @ line 1: ora-00942: table or view not exist 

i have been playing around redirecting stderr file, "cat" out in case of exception, non-error-condition output also being directed file, don't want.

in absence of sql error this, prefer direct value of count(*) shell-script variable test_count.

if, on other hand, sql raises exception, prefer direct error file, , cat file within "error encountered" condition/if clause.

is possible?

thank you.

i may missing something, have here make work!

except leave script (with exit 1):

you must not exit in error case, because error want display in result variable test_count! why not displaying too? here can propose

[...] if [ $sql_ret_code -ne 0 ]   echo "error encountered."   echo   echo "error: $test_count"   echo "abort."   exit 1 fi  echo "test_count = $test_count"  exit 0 

(hope i'm not blatantly out of game ;)


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -