Does SQLPlus exit after running a script? - windows

We are using CA Workload Control Center (Autosys) to run a windows batch file which calls another batch file which calls SQLPlus with the following:
sqlplus username/password %1 %2 %3
Since this is all being done remotely I cannot see what is actually happening on the server.
I am wondering if the sqlplus file exists but has invalid syntax (like referring to non-existent table) will the sqlplus prompt exit and return back to the batch file?
The reason I ask is because I am looking at the log file from Autosys and it shows that SQLPlus call was made, it prints out ERROR AT LINE 2: Invalid Table, but then it does not show any other activity with the batch script after that where there are multiple echoes and file copies etc. It seems as though it is not exiting SQLPlus perhaps?
Is there a parm I need to pass to SQLPlus to tell it to exit SQLPlus and return back to the calling script after running a SQL script if it fails?
Edit: We are using "WHENEVER SQL ERROR" inside of our SQL files as well and the log file does show this:
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
But I am still expecting that it should continue with the rest of the Batch script but its not, above is the last that Autosys shows in the log

See SQLPlus instruction WHENEVER SQLERROR, it describes in Oracle docs:
http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12052.htm

Found the solution SO Post:
You can do sqlplus -l test/Test#mydatabase #script.sql; the -l flag means it will only try to connect once, and if it fails for any reason will exit instead of prompting. Look at the output of sqlplus -?, or see the documentation.

Related

sqlplus does not execute the query if it is called by a ssh external connection

I have a script lying into a Unix server which looks like this:
mainScript.sh
#some stuff here
emailScript.sh $PARAM_1 $PARAM_2
#some other stuff here
As you can see, mainScript.sh is calling another script called emailScript.sh.
The emailScript.sh is supposed to perform a query via sqlplus, then parse the results and return them via email if any.
The interesting part of the code in emailScript.sh is this:
DB_SERVER=$1
USERNAME=$2
PASSWORD=$3
EVENT_DATE=$4
LIST_AUTHORIZED_USERS=$5
ENVID=$6
INTERESTED_PARTY=$7
RAW_LIST=$(echo "select distinct M_OS_USER from MX_USER_CONNECTION_DBF where M_EVENT_DATE >= to_date('$EVENT_DATE','DD-MM-YYYY') and M_OS_USER is not null and M_OS_USER not in $LIST_AUTHORIZED_USERS;" | sqlplus -s $USERNAME/$PASSWORD#$DB_SERVER)
As you can see, all I do is just creating the variable RAW_LIST executing a query with sqlplus.
The problem is the following:
If I call the script mainScript.sh via command line (PuTTy / KiTTy), the sqlplus command works fine and returns something.
If I call the script mainScript.sh via an external job (a ssh connection opened on the server via a Jenkins job), the sqlplus returns nothing and takes 0 seconds, meaning it doesn't even try to execute itself.
In order to debug, I've printed all the variables, the query itself in order to check if something wasn't properly set: everything is correctly set.
It really seems that the command sqlplus is not recognized, or something like this.
Would you please have any idea on how I can debug this? Where should I look the issue?
You need to consider few things here. While you are running the script, from which directory location you are executing the script? And while you are executing the script from your external application from which directory location it is executing the script. Better use full path to the script like /path/to/the/script/script.sh or use cd /path/to/the/script/ command to go to the script directory first and execute the script. Also check execute permission for your application. You as an user might have permission to execute the script or sql command but your application does not have that permission. Check the user id for your application and add that into the proper group.

Run batch oracle queries in multiple database

I have 4 Database connection in SQL developer(oracle). At evening I have to regularly run some scripts in all the 4 connection on same server. Is there is any shortcut that I run script only once and is reflected in all connection which is connected to some DB.
(NOTE: All the 4 connection will hold exactly same info.)
Can anyone suggest how to do this?
Here's how I do that: using the command prompt capabilities, not GUI. The only prerequisite is to have SQL*Plus installed on your computer.
For example, create SQL script you're about to run and save it into C:\Temp directory; let's name it "my_script.sql". It can contain both SQL and PL/SQL (don't forget to terminate it with a slash in that case!).
Here's how it should look like:
connect &2
spool &1
set echo on
set define off
-- Your SQL statements go here:
select to_char(sysdate, 'dd.mm.yyyy hh24:mi') vrijeme from dual;
begin
pkg_payments.p_gimme_my_money;
commit;
end;
/
-- End of your SQL statements
spool off
exit
Then create a DOS batch script, let's name it "run_my_script.bat" and put such a code in there:
#echo on
set SCRIPT=C:\Temp\my_script.sql
set SCRIPT_LOG=C:\Temp\my_script
if not exist %SCRIPT% goto END
start sqlplus.exe /nolog #%SCRIPT% %SCRIPT_LOG%.site_1.log "username_1"/"password_1"#(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host_1_name)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=service_name_1)))
start sqlplus.exe /nolog #%SCRIPT% %SCRIPT_LOG%.site_2.log "username_2"/"password_2"#(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host_2_name)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=service_name_2)))
start sqlplus.exe /nolog #%SCRIPT% %SCRIPT_LOG%.site_3.log "username_3"/"password_3"#(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host_3_name)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=service_name_3)))
start sqlplus.exe /nolog #%SCRIPT% %SCRIPT_LOG%.site_4.log "username_4"/"password_4"#(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host_4_name)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=service_name_4)))
:END
As you can see, it
sets local variables (SCRIPT, which points to a .SQL file you
created) and SCRIPT_LOG which contains log information about script's
execution)
invokes SQL*Plus (in parallel; if you want them to execute
serially, just remove the START keyword), connecting to each database
and running the script
The simplest way to test it is to double-click the .BAT file; it'll open 4 "black" command prompt windows and terminate them once the SQL script finishes its job. Check the LOG files!
When you're satisfied with the outcome, schedule .BAT file's execution in Task Scheduler so that they are executed regularly & automatically.
That would be all, I presume.

Calling an Oracle stored procedure in Informatica Cloud postprocessing command

I have an Informatica data synchronization task that creates a table in Oracle. I am trying to include a call to an Oracle stored procedure in the postprocessing command of Informatica Cloud that will update a variety of tables at the completion of the task. The procedure that I am trying to call is in the same schema as the target of the synchronization task. The procedure runs correctly when I run it directly in Oracle SQL Developer, but I can't get it to run via Informatica Cloud. I know I'm not using the right syntax to make the call, but these are some examples that I have tried so far:
BEGIN
(PROCEDURE_NAME)
END;
CALL(PROCEDURE_NAME);
EXEC(PROCEDURE_NAME);
PROCEDURE_NAME;
Would designing a mapping in Informatica Cloud help me with this? Or is there a prefix that I should be appending to the stored procedure call, even though the procedure is in the same schema as the target of the task?
None of these options will work in Synchronisation task. Call (procedure_name); will work in Mappings which is the easiest way to do it
In Synchronisation task you need to create a batch file to run a SQL script. The script has to contain the connection details.
Example:
connect user/password#
exec (procedure_name);
disconnect;
exit SQL.SQLCODE
SQL.SQLCODE is used so that it will commit all the transactions.
After that, it will be necessary to use a .bat to run the above sql script using sqlplus client.
Example:
cd \bin
call cmd.exe /C "sqlplus /nolog < {path to sql script created in step
2}\filename.sql"
exit /b 0;

How to make a sqlplus quit when database is not available?

i have a problem with sqlplus on windows batch.
I have SQLPLUS 10.2
i'm trying to connect to a database through a windows script.cmd
script.cmd only launches : sqlplus test/Test#mydatabase #script.sql
The problem is when the database is not available, the sqlplus says
ERROR:
ORA-12541: TNS:no listener
Enter user-name :
and waits for input .. and blocks the .cmd
How can i adapt the script to stop immediately when the database is not avaliable or just to avoid waiting for user prompts ?
Thanks
You can do sqlplus -l test/Test#mydatabase #script.sql; the -l flag means it will only try to connect once, and if it fails for any reason will exit instead of prompting. Look at the output of sqlplus -?, or see the documentation.
may be you should think about using TNSPING utility. You can use it within CMD script before trying to connect to db with sqlplus. In this case you should analyse its output.
You can try with 2 connects.
First one will spool result to a file.
In the second one check what is in that file. If you don't encounter ORA-12541 or any other error messages then call the second script.
You can make all these commands inside one batch script and call it with
SQLPLUS #script.sql
and inside use
connect test/Test#mydatabase

What is the use of TNS_ADMIN variable in Oracle?

Please tell me what is the use of TNS_ADMIN parameter in Oracle? I am working on Unix using oracle database.
Is this parameter is required to locate the sqlplus. I am executing a script in which a update query is executed on Oracle Database.
The script fails with 127 error code when executed with crontab.
The script contents I suspect (eval) failing are
----------
cmd='sqlplus ${ORALOGIN} < SQLS
----------
eval $cmd
TNS_ADMIN tells sqlplus where to find the tnsnames.ora file.
If you are running sqlplus from a crontab then the normal reason for having difficulty are:
Incorrect path
Not having the correctly set ORACLE_SID or other Oracle connection information
A startup/login script that is getting executed when you login to the system that is interfering with your cron execution
Some script that you run from the command line when you login that sets up your Oracle environment that is not getting executed in your crontab.
Check these things and other environment related items. It always takes me a number of passes to get crontab and Oracle to work happily together.

Resources