How to Make Sql Plus Exit with an Error Code


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to Make Sql Plus Exit with an Error Code
# 1  
Old 06-16-2011
How to Make Sql Plus Exit with an Error Code

Dear all,

How to make sqlplus command to exit with an apt error code in bash script,
It always returns 0 for me.

Thanks
# 2  
Old 06-16-2011
Under what circumstances would you like it to exit with an error (sorry, not sure what you mean by "apt error"!)?

You could, perhaps look at:
Code:
SQL> help whenever

 WHENEVER OSERROR
 ----------------

 Performs the specified action (exits SQL*Plus by default) if an
 operating system error occurs (such as a file writing error).
 In iSQL*Plus, performs the specified action (stops the current
 script by default) and returns focus to the Workspace if an
 operating system error occurs.

 WHENEVER OSERROR {EXIT [SUCCESS|FAILURE|n|variable|:BindVariable]
                   [COMMIT|ROLLBACK] | CONTINUE [COMMIT|ROLLBACK|NONE]}


 WHENEVER SQLERROR
 -----------------

 Performs the specified action (exits SQL*Plus by default) if a
 SQL command or PL/SQL block generates an error.
 In iSQL*Plus, performs the specified action (stops the current
 script by default) and returns focus to the Workspace if a SQL
 command or PL/SQL block generates an error.

 WHENEVER SQLERROR {EXIT [SUCCESS|FAILURE|WARNING|n|variable|:BindVariable]
                    [COMMIT|ROLLBACK] | CONTINUE [COMMIT|ROLLBACK|NONE]}

i.e.
Code:
$ cat somesql.sh
sqlplus -s scott/tiger << !
  whenever sqlerror exit 2;
  select 1 from blah;
!

echo Exited with $?

$ ./somesql.sh

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


Exited with 2

This User Gave Thanks to Scott For This Post:
# 3  
Old 06-16-2011
Let me make it clear

It always returns ZERO for me
Actually i have executing a "sqlplus" command with the params as db username, pwd schema and the pl/sql file[which is having procedure] in -s mode.

In case, if procedure in a file have thrown an exception i need to block some code of execution in a script.

how do i return the exit code post execution of query via sqlplus command.

Suppose i have 3 procedures to be executed, each one are interdependent
proc_1.sql
proc_2.sql
proc_3.sql

if, the exception occured in proc_2.sql then i should not execute proc_3.sql. so i need to have a control over the code based on the exit code after each sqlplus command exectuion.


Thanks every1.
# 4  
Old 06-16-2011
I'm somewhat confused by your use of terminology.

I would consider an "sqlplus" command to be something like "set", "show", etc.

Do the "proc_1.sql", etc. contain SQL, or PL/SQL; DML or DDL?

PL/SQL has exception handling, for example using RAISE, EXCEPTION and WHEN.

Please post one of the "proc_*.sql" scripts.
# 5  
Old 06-16-2011
Sincere apologies for the confusions, am relatively new to this forum and unix Smilie

Proc_*.sql will have pl/sql statements in it. I can able to handle the exception there.


I am doing in all these operations in a single .sh file
Running sqlplus for proc_1 -
then,
sqlplus for proc_2 -
then,
sqlplus for proc_3

Now, the scenario is i need to run the scripts only if the previous procedure ran successfully.
so, if proc_2 is got exception while executing its pl/sql statements then i need to skip the execution of proc_3.
For that i took the value of $? post execution of sqlplus command. But it alwyz returns zero to me irrespective of the exception thrown after the pl/sql execution

Thanks a ton for your patience. Smilie
# 6  
Old 06-16-2011
You still didn't post any PL/SQL Smilie

Maybe this will (or won't!) give you some idea.

Code:
# Set some stuff up:
$ sqlplus -s scott/tiger

SQL> create table test1( a number(10));

Table created.

SQL> insert into test1 values( 1 ); -- (***) note the value!

1 row created.

SQL> commit;

Commit complete.

create or replace procedure testproc1 is
  X number;
begin
  select a into X from test1 where a = 1; -- (***)
exception
  when no_data_found then
    raise;
end;
/
create or replace procedure testproc2 is
  X number;
begin
  select a into X from test1 where a = 3; -- (xxx)
exception
  when no_data_found then
    raise;
end;
/
create or replace procedure testproc3 is
  X number;
begin
  select a into X from test1 where a = 4;
exception
  when no_data_found then
    raise;
end;
/

^D

$ cat SomeTest
sqlplus -s scott/tiger << !
whenever sqlerror exit 3;
select 'Test 1' from dual;
exec testproc1;               -- This works (*1 - it selects where value = 1 (***))
select 'Test 2' from dual;
exec testproc2;               -- This fails (*2) (xxx)
select 'Test 3' from dual;
exec testproc3;               -- This is never executed (*3)
!

echo Returned $?



$ ./SomeTest
'TEST1
------
Test 1


PL/SQL procedure successfully completed. (*1)


'TEST2
------
Test 2

BEGIN testproc2; END;

*
ERROR at line 1: (*2)
ORA-01403: no data found
ORA-06512: at "SCOTT.TESTPROC2", line 8
ORA-06512: at line 1

(*3) Note that TESTPROC3 is not executed

Returned 3

# 7  
Old 06-16-2011
@vetrivendhan

I think what you really want, is to run sqlplus, send all the output to a log file, then search the log file for known SQLPlus errors.

SQLPlus always returns zero, because the command always works. It may not do what you want, but the unix command does not fail.

ex:
if $STANDARD_OUTPUT_MESSAGES_FILE is my log file:
Code:
   grep 'ORA-[0-9][0-9][0-9][0-9][0-9]:' ${STANDARD_OUTPUT_MESSAGES_FILE} | egrep -vi "testing|ORA-01034: ORACLE not available|SYS.DBMS_METADATA|OUT bind variable bound to an IN position|unexpected error from call to export_string|ORA-01918|ORA-31684|MGMT_METRICS_RAW|ORA-06512: at line|ORA-01917: user or role |ORA-39082|LibraryCacheNotEmptyOnClose" >>$ORACLE_ERROR_MESSAGES_FILE

   grep 'SP2-[0-9][0-9][0-9][0-9][0-9]:' ${STANDARD_OUTPUT_MESSAGES_FILE} >>$ORACLE_ERROR_MESSAGES_FILE

   grep 'PLS-[0-9][0-9][0-9][0-9][0-9]:' ${STANDARD_OUTPUT_MESSAGES_FILE} >>$ORACLE_ERROR_MESSAGES_FILE

   grep 'UDE-[0-9][0-9][0-9][0-9][0-9]:' ${STANDARD_OUTPUT_MESSAGES_FILE}  >>$ORACLE_ERROR_MESSAGES_FILE

   grep 'LRM-[0-9][0-9][0-9][0-9][0-9]:' ${STANDARD_OUTPUT_MESSAGES_FILE}  >>$ORACLE_ERROR_MESSAGES_FILE

   grep 'Job "SYS"."SYS_IMPORT_SCHEMA_[0-9][0-9]" stopped due to fatal error at' ${STANDARD_OUTPUT_MESSAGES_FILE}  >>$ORACLE_ERROR_MESSAGES_FILE

   grep -i ' not found in ' ${STANDARD_OUTPUT_MESSAGES_FILE} | egrep -vi "ARA_FIL.OPS_PART_TRACKING_LOG" >>$ORACLE_ERROR_MESSAGES_FILE

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. UNIX for Advanced & Expert Users

Exit when sql script give error

Hi Guys, I am calling a SQL script which runs under while loop, i need to exit the while loop if i get error in sql script which is called while do sqlplus -s user/pass@db @test.sql id$i done test.sql whenever sqlerror exit; alter table t1 add &1 number; I need to come out of... (2 Replies)
Discussion started by: rohit_shinez
2 Replies

3. Shell Programming and Scripting

Make expect exit the UNIX script in erreneous condition

Hi, I am writing a menu driven program using shell script. THe script will be collecting data by logging into the other servers and bringing back the data to home server to process it and accordingly issue commands. TO automate commands execution , I am using expect script. However I am not able... (5 Replies)
Discussion started by: ashima jain
5 Replies

4. Shell Programming and Scripting

How to grep sql error in shell script and exit the script?

I need help in the following script. I want to grep the sql errors insert into the error table and exit the shell script if there is any error, otherwise keep running the scripts. Here is my script #!/bin/csh -f source .orapass set user = $USER set pass = $PASS cd /opt/data/scripts echo... (2 Replies)
Discussion started by: allinshell99
2 Replies

5. Solaris

Gani Network Driver Won't Install - make: Fatal error: Don't know how to make targ...

I attached a README file that I will refer to. I successfully completed everything in the README file until step 4. # pwd /gani/gani-2.4.4 # ls COPYING Makefile.macros gem.c Makefile Makefile.sparc_gcc gem.h Makefile.amd64_gcc ... (1 Reply)
Discussion started by: Bradj47
1 Replies

6. Shell Programming and Scripting

top's exit code indicates error--is this a bug?

This single line of code in a sh script file top -b -n 1 -U $USER causes the script to prematurely exit with an exit code of 1 (i.e. an error) if the script is run with the -e option (e.g. if set -e is executed near the top of the script file). Alternatively, you can execute it like top... (8 Replies)
Discussion started by: fabulous2
8 Replies

7. Linux

Error in issuing a make and make install

Hi, Recently I install a package and try to do a make and make install. However, in the make it gives me below error:- make:Nothing to be done for 'install-exec-am' make:Nothing to be done for 'install-data-am' Can anyone please explain to me what does this mean? I have been trying... (1 Reply)
Discussion started by: ahjiefreak
1 Replies

8. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies

9. Shell Programming and Scripting

All about exit code

Hi, I am working on Solaris 8 and the "intro" man page says, "Upon termination, each command returns two bytes of status, one supplied by the system and given the cause for termination, and (in the case of 'normal' termination) one supplied by the program. The former byte is 0 for normal... (2 Replies)
Discussion started by: cdin2
2 Replies

10. Shell Programming and Scripting

sql error code trapping

Hello #!bin/ksh sqlplus -s system/manager < |grep '^ORA' |uniq select * from kk; set echo on show spool on end; / EOF save test.sh sh test.sh results ORA-00942: table or view does not exist (3 Replies)
Discussion started by: xiamin
3 Replies
Login or Register to Ask a Question