[Last Call] Learn how to a build a cloud-first strategyRegister Now


unix + oracle sql code

Posted on 2014-08-04
Medium Priority
Last Modified: 2014-08-29
In shell script I have one oracle sql block . Suppose the sql block gives ant oracle exception.Then we stop the process.

I know in oracle how to stop it. e.g. I have used Exit keyword and come out from sql block.

But in unix how can we stop the process if sql error occurs.
How will user know the error e.g. no_data_found

Please Give me the examples of unix shell script and in that they call oracle sql code.
Question by:digs developer
  • 3
  • 3
  • 2
  • +1
LVL 38

Accepted Solution

Gerwin Jansen, EE MVE earned 596 total points
ID: 40240022
>> In shell script I have one oracle sql block
Please post (part) of your script with the sql block

If you use sqlplus in your unix shell script then you can use the exit code from sqlplus to check for in your unix shell script.

For example, your somesql.sh looks like:

sqlplus -s scott/tiger <<EOFSQL
  whenever sqlerror exit 2;
  select 1 from not_existing_table;
echo Exited with $?

$ ./somesql.sh

  select 1 from not_existing_table
ERROR at line 1:
ORA-00942: table or view does not exist

Exited with 2
LVL 35

Expert Comment

ID: 40240052
I prefer to use the following:

whenever sqlerror exit sql.sqlcode;

That way the return code is the error code that caused the exit.

I'm not sure that a NO_DATA_FOUND would trigger this.  I believe that is considered a warning and not an error.  If you trap it and raise it as a user defined error, it should pick it up.
LVL 40

Assisted Solution

omarfarid earned 592 total points
ID: 40240419
In unix, processes will exit with status 0 if successful. The exit status of a process or program can be checked by shell env variable $?. if $? is not 0 then there would be an error (this is set by the program / process that was running, and you need to check what sqlplus would return as exit status and what it means).

an example shell code to check exit code of a command / process:

if [ $? -eq 0 ]
      echo "last command exit status is successful"
     echo "last command exit status needs to be checked"
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 35

Assisted Solution

johnsone earned 592 total points
ID: 40240895
SQL*Plus will always return 0 unless otherwise directed by the user.  I believe the only time you would get a non-0 exit status is if the program itself encounters an error, but personally I don't think I have ever seen that.

According to the documentation I found the default behavior of 2 the exit strategies (OS error or SQL error) is to return a success code.

The WHENEVER clause is the key, without that processing wouldn't stop on an error and SQL*Plus wouldn't exit.

Author Comment

by:digs developer
ID: 40267727

Author Comment

by:digs developer
ID: 40278901
I've requested that this question be closed as follows:

Accepted answer: 0 points for digvijay yadav's comment #a40267727

for the following reason:

LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40278902
I believe this question is closed incorrectly, 0 points for the askers own comment?
LVL 35

Expert Comment

ID: 40282724
I would say an even split between these 3 comments:

http:#a40240022 - 149 points
http:#a40240419 - 148 points
http:#a40240895 - 148 points
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40285280
@johnsone - I agree

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Fine Tune your automatic Updates for Ubuntu / Debian
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question