start oracle .sql script through SQLcl on a remote windows host - oracle

On an Oracle database server, I have been able to schedule a nightly job that runs oracle scripts initiated from a powershell script which has this line:
sqlplus accountID/password #C:\scripts\run_scripts.sql
Now I need to achieve the same on another windows machine where the only thing of Oracle is the SQLcl client. I am able to invoke the initial_script.sql SQL script, but only interactively so far through three steps:
PS C:\sql_scripts> sql /nolog
SQL> connect id_maint/password#111.22.33.44:1521/sid
Connected.
SQL> #initial_script.sql
What I need is to have the oracle initial_script.sql script started from powershell. I have two hurdles.
To connect to the remote database, I have to first get the SQLcl's SQL> prompt via the PS> sql /nolog command at the powershell command prompt, and then run the SQL> connect id_maint/password#111.22.33.44:1521/sid command at the SQL> prompt.
SQL> connect id_maint/password#111.22.33.44:1521/sid works fine. But I need to invoke the oracle script initial_script.sql at the same time. However,
SQL> connect id_maint/password#111.22.33.44:1521/sid #C:\sql_scripts\initial_script.sql
incurs an error.
So what I am seeking is to get three things done (get the SQL> prompt -> connect to the remote database -> invoke the oracle script), all initiated by a power script at the PS> prompt.
Appreciated it very much if someone can help me out.

SQLcl is 99% compatible with SQLPlus so for almost any sqlplus command you should be able to just use sql.
Launch the script using your existing sqlplus command but using sql instead
sql id_maint/password#111.22.33.44:1521/sid #C:\sql_scripts\initial_script.sql

Related

Understanding connecting to the database through SQLPLUS

when reading the Oracle APEX documentation, I did not understand the reason it asks to connect to the database through sqlplus using sqlplus /nolog then connect sys as sysdba. If it will eventually connect with sys as sysdba, why not doing it from the beginning with sqlplus sys as sysdba command?
Here is what I am talking about:
https://docs.oracle.com/en/database/oracle/application-express/20.1/htmig/downloading-installing-Oracle-AE.html#GUID-7E432C6D-CECC-4977-B183-3C654380F7BF
I would suspect its historical. In older releases on SQL Plus, if you ran:
sqlplus my_user/my_pass
on the command line, then on unix environments someone running the 'ps' command could see those details. Even in modern versions of SQL Plus where we remove that from obvious view, then digging into things like command history could expose it.
So always best never to type user or password details on the command line.

I want to schedule an Oracle Sql script to run on the database server

I want to schedule an Oracle Sql script to run using Sql*Plus on my database server at midnight.
Currently I run it manually using:
nohup sqlplus username/password #myscript.sql &
How can I schedule it? I don't want to pass username and password openly. Is there a way to hide it?
One way of 'hiding' your password:
In the script do a
CONN username/password
Then run your script as:
sqlplus /nolog #script_name

how to connect to multiple oracle databases of different servers via shell script

I want to connect to multiple (30) oracle databases which resides in different servers (27) via shell script and fetch details from each database. I have a user (test) created on all databases but the details I want to fetch needs sysdba privileges. In my environment, I cannot provide DBA privs to any other user due to limitations. Hence my idea is to connect to each database using sqlplus -s "$user/$password#$tnsentry" and then connect as sysdba to fetch details.
Though I am able to connect to all databases using test, the "connect as sysdba" is getting executed on current database in current server.
My script:
cat tmp/db.par | while read LINE
do
if [ -n "$LINE" ] ; then
tns_entry=$LINE
export tns_entry
sqlplus -s /nolog >> $tmp/query.log <
exit
sqlconn
fi
done
In the tns_entry loop, I have given ABC,DEF,GHI,JKL,MNO databases to get details from and I am running this query from a server where database XYZ resides. I didn't give XYZ in my loop and didn't connect to database but the query results are from XYZ database. Please help me on this.
Thank you!
You should connect as sysdba
add "as sysdba" in the script and try.
conn sys/password#tnsentry as sysdba

Running Sybase SQL commands from DOS/Windows batch file

Is there any way I can run Sybase SQL commands from command prompt. I need to write a batch file which runs an SQL query on machine as a fix for a bug.
Use ISQL. See http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.utility/html/utility/utility10.htm for the complete reference.
Specifically, you're looking for commands like this:
ISQL -S server -D database -U user -P password
This will launch ISQL with the indicated settings, and from there you can run SQL statements against the database.

Should CONNECT work in SQL*PLUS script?

I'd like to run a sqlplus script from a cron job.
I thought I could put a line like:
CONNECT "myuser/mypass#mydb"
within the script and then just execute it with:
sqlplus #myscript
However, when I do so, I get:
SP2-0306: Invalid Option
SP3-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus
Am I misunderstanding the usage of the connect command?
When running CONNECT inside SQL*Plus, remove the quotes:
CONNECT myuser/mypass#mydb
They double quotes are required if you are passing the credentials as an argument to sqlplus:
sqlplus "myuser/mypass#mydb"
, for the shell to parse myuser/mypass#mydb as a single argument if you have spaces in your connection identifier or use additional options like AS SYSDBA.
Use the /NOLOG option.
sqlplus /nolog #myscript
Oracle 11gR2
I ran a .sql file via SQL*Plus connected initially as JOHN. Within the file I connect as SYS and then run a GRANT. See .sql file contents below:
connect sys/password as sysdba
GRANT EXECUTE ON DBMS_CRYPTO TO JOHN;
connect JOHN/DOE
NOTE: I don't recommend keeping the sys/password in a text file btw.
HTH

Resources