1

I have a set of update statements which needs to be executed one after the other. The database that i am using is Oracle and i am running these queries by connecting to the database using a shell script. What i want to know here is to how i could execute these commands one after the other only if the previous update statement is successful. The auto update is set to OFF in the shell script and i want to commit all the statements only when all of them are executed successfully else i want to exit. Is there a way to run these update commands one after the other by checking the preceding queries status and commit after all the queries are run successfully.

7
  • Are you using Oracle or MySQL? Commented Sep 27, 2017 at 6:55
  • use if else block Commented Sep 27, 2017 at 6:59
  • It is oracle DB Commented Sep 27, 2017 at 8:05
  • update * from table_name where condition; Commented Sep 27, 2017 at 8:05
  • update * from table_name where condition;update * from table_name where condition;update * from table_name where condition;update * from table_name where condition;update * from table_name where condition; Commented Sep 27, 2017 at 8:06

1 Answer 1

2

you can use WHENEVER SQLERROR EXIT ROLLBACK:

sqlplus -s username/password@network-name <<EOF
   WHENEVER SQLERROR EXIT ROLLBACK
   update table1 set mycolumn=value where mycolumn=1;
   update table1 set mycolumn=othervalue where mycolumn=2;
   update table1 set mycolumn=othervalue where mycolumn=3;
   update table1 set mycolumn=othervalue where mycolumn=4;
   commit;
   exit
EOF

Here is a link to the documentation:

https://docs.oracle.com/database/121/SQPUG/ch_twelve052.htm

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

2 Comments

This is exactly what i am looking for. Thanks mate :)
great ! consider accepting the answer if it resolved your problem.

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.