Oracle - how to stop script execution after any error? - oracle

For example, let's say we have the following script to execute in prod.
drop index idx_test1;
drop index idx_test2;
But, after execute first instruction we get the error: ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired. From here I'd like to stop all subsequent instructions. Is there any way to do it in sqlplus and sqldeveloper/PL/SQL Developer?

I would recommend running scripts using Sql*Plus. You would use
WHENEVER SQLERROR EXIT SQL.SQLCODE ROLLBACK
This can be part of your glogin.sql or just as a header of your production script.
This will also send the error code to the calling process for your own logging so if you have some orchestrator, it can know what's happened.

Related

How to avoid series of commands running under sql plus using shell script

I have created a script which runs a series of other instances in shell using the while command. But the problem is that one of the job is connecting to sql plus and running the remaining command under sqlplus.
For ex. my input file --> job.txt
job1
job2
job3
Now, using the below script I am calling job one by one. So, until the present job is finished next job wont start. But the catch comes when the job tries to connect the sql plus. After it connects to sql plus, the process id of the current job gets complete and remaining jobs run as the sql statement in unix env.
while read line;
do
$job1
done < job.txt;
Error message I am getting in sqlplus after current instance exits.
SP2-0042: unknown command
How can I avoid bringing the jobs under sqlplus.

How to call ODI Scenario from command line and wait for its execution

I'd like to call an ODI Scenario from command line and wait until its done. I am using ODI 12c and installed a standalone agent. I already found out that you can use the startscen.cmd command and it works for me. The only problem is that cmd is not waiting for the scenario to be done. Any Suggestions to achieve sth like that?
My .bat-file looks like this:
cd C:\Oracle\Middleware\Oracle_Home\user_projects\domains\base_domain\bin
call startScen.cmd "-INSTANCE=CITestAgent" MAPPING 1_0_0 GLOBAL "-SESSION_NAME=TEST_RUN" "-AGENT-URL=http://localhost:20910/oraclediagent"
cd C:\Users\Redekera\documents\testFiles
"C:\Users\REDEKERA\Documents\instantclient_19_3\sqlplus.exe" db_user/pw#db/scheme #run_tests_lieferschein.sql
After that command i'd like to run an sql via sql*plus, which needs to wait until the scenario has finished.
Thanks for help guys :)
By default startscen.cmd will wait for the end of the execution to return.
This can be changed with parameter -ASYNC=yes to start the execution asynchronously. In that case it would return the SESSION number that is useful to check the status of execution.
If you want the second command to execute only if the first exited successfully:
execute scenario command && sql*plus command
Extracted from here
The main idea is the “&&” sign!

Why doesn't Oracle reset processes when I restart the database

I am getting Oracle maximum processes exceeded error. To increase the the maximum allowed processes I log in as sysdba:
$ sqlplus / as sysdba
I try to see processes parameter:
sql> show parameter processes
ERROR:
ORA-01012: not logged on
I presume that sqlplus is not able to login due to processes being exceeded.
I try to shutdown the instance so that processes will be closed:
SQL> shutdown immediate
ORA-24324: service handle not initialized
ORA-24323: value not allowed
ORA-00020: maximum number of processes (%s) exceeded
I try to kill the Oracle service, that should kill the processes:
$ sudo service oracle-xe restart
[sudo] password for kshitiz:
Shutting down Oracle Database 10g Express Edition Instance.
Stopping Oracle Net Listener.
Starting Oracle Database 10g Express Edition Instance.
I login as sysdba again but it gives the same error.
How do I kill the processes so that the database is manageable again? And how should I diagnose why this error is occuring (which application is responsible for hogging the database)?
I am on Oracle 10g express and Ubuntu 13.10.
The solution is to increase the max number of processes allowed. But the problem is that in this state there is no way to do that since DB wouldn't take any commands. Even the commands to increment the process parameter would give the maximum processes exceeded error.
Kill all processes belonging to oracle user:
$ sudo su
$ su oracle
$ kill -9 -1
Check that all process are killed:
$ ps -ef | grep oracle
If not kill them using kill -9 PID. Now connect to database as sysdba:
$ su oracle
$ sqlplus / as sysdba
Shutdown the database (1. Shutdown immediate would probably not work and give same ORA-01012 error. 2. Since the processes are killed isn't the database already shutdown? That's what I thought but it seems that it keeps a record of the last state or something which isn't clear out until you run the following command):
SQL> shutdown abort
Now bring it up again:
SQL> startup
Modify the processes parameter to prevent this problem from recurring:
SQL> select count(*) from v$process;
SQL> alter system set processes=300 scope=spfile;
Restart database:
SQL> shutdown immediate
SQL> startup
Check that max processes have increased:
SQL> show parameter processes
Edit:
After all of this for some strange reason my application wasn't connecting to DB, though SQL plus was working just fine. So as a last step I restarted the oracle service:
$ sudo service oracle-xe restart
This got everything working for me.

What is the shell command for waiting the service to finish starting?

I need a simple batch command which will wait until specified service (actually, SQL Server) is finished starting. See, the bat file runs some non-service executables which attemp to connect to the SQL server on start. And they fail.
I tried to help (" /?" key) some Windows Shell commands but they do not seem to respond with an action I need.
Unfortunately, this command doesn't exist. The sc can start and stop a service and it can query the status of a service (see this answer). But even when you query, you will only get "Service is running"; there is no way to tell how far the service got in its startup.
There are two workarounds:
Sleep for a while
Run a simple SQL command in a loop
The first approach will help but it won't completely solve the problem; when the server is under load, the startup can take longer.
For the second approach, use a loop which times out (pseudocode):
Set counter to X
Try to connect
If connect succeeded, exit with success
Decrement counter
If counter <= 0 -> ERROR
Sleep 1s
Go to step 2
For additional safety, you could add a sc query in there to make sure the service didn't fail.

JDBC Batch update

IF i have say 1000 statements in a batch.If one of the command fails to execute properly then will all the remaining commands execute or execution stops at that point itself??
Execution stops at that point. Read this post for a solution/workaround : BatchUpdateException: the batch will not terminate

Resources