How to startup automatically Oracle 19C using bash scripting and SQL PLUS? - bash

I've tried to create a bash script for connecting to my Oracle 19C database using sqlplus, I added the following code (which works also) :
#! /bin/sh.
connectsqlplus(){
sqlplus "'"sys/mypassword as sysdba"'"
EOF
}
connectsqlplus
The main problem is that I want to use this bash script to start the database, what I've created seems to only let me open the sqlplus terminal, I just want to add the SQL> startup so that the database starts but I'm not really sure how to add this command in the SQL terminal by using bash scripting.
Any help would be much appreciated ! Thank you .

#start_oracle_db.sh
ORACLE_SID=nameofyourdatabaseasrecordedinoratabfile
ORAENV_ASK=NO
source oraenv
sqlplus << EOF
connect / as sysdba
startup
exit
EOF

Related

Trying to execute sql script using sqlplus from powershell commandline

I'm trying to execute a sql script from powershell using the following command:
sqlplus username/password#tnsnamesalias 'path to my sql file.sql'
If I run the command without the script path, I can connect to the database and execute commands. If I include the script path (which includes spaces) then I just get the sqlplus help text and no changes are made to the database. My sql script is finished with END; and /
What am I doing wrong?
I believe you need to add the # sign before the path:
sqlplus username/password#tnsnamesalias #'path to my sql file.sql'
You have forgotten "#" symbol and the apostrophes are wrong here.
This works for me for executing a "test script.sql" file
sqlplus .... "#test script.sql"

How to execute SQL from Oracle SQL Plus passing variable name SQL file from Unix

For Oracle DB and HP-UX :
I have a variable name SQL file like: my_sql_file_${TIMESTAMP}.sql
I assign this file to a variable: MYSQLFILE="my_sql_file_${TIMESTAMP}.sql"
So, I am trying to execute the MYSQLFILE inside SQL Plus, like:
#${MYSQLFILE}
but this appears that it cannot be done.
Is there any way to achieve the above??
Thank you.
This should work:
MYSQLFILE="my_sql_file_${TIMESTAMP}.sql"
sqlplus -S user/password#db << EOF
#$MYSQLFILE
exit;
EOF
You can also pass user, password and db as parameters to your script
you can use
sqlplus user/pass#service_name #$MYSQLFILE
inside SQLPLUS, you can also use
sql> host echo $MYSQLFILE
And this is also valid,
sqlplus user/password#db <<EOF
#$MYSQLFILE
exit;
EOF

Exit from the oracle user session when running a script

I have a problem with a bash script on an Ubuntu system. I need to program a script which connects to oracle an spool some queries to text files.
Almost all of this tasks are acomplished but the script doesn't run as it should be.
To generate the connection to oracle I use the following lines inside my script:
su oracle
export ORACLE_SID=DB_SID
export ORACLE_HOME=[ORACLE_PATH]
export TWO_TASK=[HOSTNAME:PORT]
export PATH=$ORACLE_HOME/bin:$PATH
sqlplus -s usr/pass << EOF
In the subsequent lines I make all the spooling of the data and finally for exiting from sqlplus I use the following lines:
quit;
To diconnect from sqlplus
exit
To exit from the su oracle session
EOF
And after the EOF tag I put some other commands to be executed.
Problem is when I run my script:
user# sh MyScript.sh
Instead of doing all the tasks, the script only executes the lines to the point of the << EOF and returns to me the control of the terminal still logged as the oracle user.
oracle#user#
If I type 'exit' and press enter then the rest of the script is executed.
I need the script to execute from start to finish without this middle step but I don't know how.
Thanks in advance.
In the end it's not neccesary to use the [su oracle] to make the export of the environment variables so I simply deleted the line:
su oracle
And the process worked like a charm.
Thanks for all who helped.

Execute SQL query on Oracle working in Unix prompt but not in shell script

I am trying to connect to an Oracle db using a ksh script. When I run this directly from the prompt, it works. But when I put it inside a script (abc.sh) it fails. Below is what I've put in the script (edited to make it shorter):
Here abc is the username, while abc$123 is the password of the user which has access on database DBNAME.
#!/usr/bin/ksh
sqlplus -s /nolog << EOF > output
connect abc/abc$123#DBNAME;
set echo off
set heading off
select table_name from dba_tables;
exit;
EOF
This works if typed directly, but run as ./abc.sh, gives error -
ERROR ORA-01017: invalid username/password; logon denied
I'm sure I'm missing something simple, but can't figure this out. Thanks for your help.
You cannot just write code like this and expect it to be passed to the relevant program. Your SQL lines are being executed by KSH instead of your SQL server. Instead you need to do something more along the lines of:
echo "SQL CODE HERE" | sqlplus ....

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

Resources