0

How can I error handling in SQLPlus , printing out customised messages when an error is encountered. I have put my code below.

My Code

#!/bin/bash

    echo "My Scripts run below"
    sqlplus -S UID1/UID2@DB1<< EOF
    whenever sqlerror exit sql.sqlcode;
    @/path/Script1
    @/path/Script2
    exit;
    EOF
   echo "My Scripts have run"

Output

My Scripts run below
SP2-0310: unable to open file "/path/Script1.sql"
SP2-0310: unable to open file "/path/Script2.sql"
My Scripts have run

Required Output

My Scripts run below
**Below error in Script1**
SP2-0310: unable to open file "/path/Script1.sql"
**Below error in Script2**
SP2-0310: unable to open file "/path/Script2.sql"
My Scripts have run

2 Answers 2

1

Indeed, SQLPlus return code is always 0 fr om my experience, so I advise you to do what I had to do in previous projects: redirect scripts output in a file, then parse it to find SPx-xxxx or ORA-xxxx expressions that indicate errors.

Sign up to request clarification or add additional context in comments.

Comments

1

WHENEVER SQLERROR detects an error in a SQL command or PL/SQL block. You should use WHENEVER OSERROR instead to catch operating system errors.

WHENEVER OSERROR EXIT FAILURE

If in doubt which error code is returned, you could think about hardcoding a number:

WHENEVER OSERROR EXIT 1

For clarity, I'd change the last EXIT to

EXIT SUCCESS

You won't be able to catch both errors for script1 and script2. After the first error, your SQL*Plus scripts exits and hands back control to bash.

About what to do with the exit code back in bash, see Error handling in Bash

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.