U of A University of Arkansas Division of Agriculture

Pictures of chickens, flowers, wheat, a boy looking through a magnifying glass, irrigation pipe, soybean pods, and fruits and vegetables.

Cooperative Extension Service

Cooperative Extension Service

Agricultural Experiment Station

Steve Rea's
Oracle Tips, Tricks, and Scripts

Link to Steve Rea's Oracle Certification graphic


Search | Publications | Jobs | Personnel Directory | Links

Returning Status Code From SQL Script To UNIX Shell

This tip was in the May/June 2001 issue of Oracle Magazine (pg 20) (no longer available).  To return a status code of over 0 (which usually indicates no error) up to 255, you can use the "raise_application_error" function, passing -20224-status to it, along with the "whenever sqlerror" directive, to return the code back to the UNIX shell.  In the program below, a status of 55 is returned (such as from a function call or used directly in the script itself) to the shell, which can then be tested in a shell script to continue or terminate the processing in that shell script.  Note that for AIX UNIX, the return code is in the "$?" environment variable.  Other unix versions may use "$status" or "$rc" or some other return code variable.  Also, if you want to see the resulting message ("Bummer"), just remove the ">/dev/null" redirection.

sqlplus -s myuserid/mypswd <<EOF >/dev/null
variable rc number
whenever sqlerror exit sql.sqlcode
begin
     -- Below is really your function call returning a status code
     -- of 55 to the rc (return code) variable, which can range from
     -- 0 (no error) to 255 (only one byte can be returned to shell).
     :rc := 55;
     if (:rc <> 0)
     then
          raise_application_error(-20224-:rc,'Bummer');
     end if;
end;
/
EOF
export x=$?
echo $x

This Page was Last Updated on 06/04/07

You Are Visitor Number

Hit Counter

Link to CES Home PageBack To CES Home Page

Link to Steve Rea's Oracle PageBack To Steve Rea's Oracle Page

Back to top of this pageBack To Top


© 2006
University of Arkansas
Division of Agriculture
All rights reserved.
Last Date Modified 02/09/2008
Webmaster

University of Arkansas • Division of Agriculture
Cooperative Extension Service
2301 South University Avenue
Little Rock, Arkansas 72204 • USA
Phone (501) 671-2000 • Fax (501) 671-2209
 

MissionDisclaimerEEO
PrivacyFOI