I am trying to understand a simple script that connects to Oracle database using sqlplus command in unix:
1 sqlplus -s /nolog > /dev/null 2>&1 <<EOF
2 whenever sqlerror exit failure
3 connect $user_pwd
4 exit success
5 EOF
If I am using unix then I use commands as sqlplus $user_pwd for connecting to oracle database and to come out of sqlplus command I use exit. Please help me in understanding the lines 1,2,4,5. It may be a simple question for experts but I am not able to understand when to use these.
-s is for silent mode (not output anything)
> /dev/null 2>&1 things are for force to not display anything. (redirect standard output and standard error to /dev/null)
/nolog is for not trying to login with the command line arguments. (the login credentials aren't provided here.)
<<EOF is a heredoc input redirect. The lines until EOF will be passed to sqlplus as standard input. (So this is why the last line is EOF)
About the whenever line: http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12052.htm So the command's return value will be failure if something wrong happens.
connect $user_pwd connects the sqlplus to the server
The exit success makes sqlplus returning will success. http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12023.htm#i2697968
Related
Trying to connect to ORACLE SQLPLUS using unix shell script. But it is getting failed..
Looks like the script in line 3 is incorrect as I am passing username, password and SID
#!/bin/sh
cd /dev/shrd/alt/test1/stest/ptest
V1=`sqlplus testuser/passwd#testSID <<EOF
SELECT count(*) FROM test_table WHERE region='Aus';
EXIT;
EOF`
if [ -z "$V1" ]; then
echo "No rows returned"
exit 0
else
echo $V1
fi
I got an error stating -ORA-12162: TNS:net service name is incorrectly specified when I added - sqlplus $username/$password in the script.
Can anyone please confirm if the below syntax is valid and I can add it in shell script?
> sqlplus MyUsername/MyPassword#MyHostname:1521/MyServiceName
Kindly guide me if I'm missing something (like Hostname, Port Number,TNS_entry or something else).
Thanks in advance :)
Until you are successful in obtaining any output from your sqlplus command, you should not use "-S". Without that, sqlplus will provide you with much-needed error-reporting/feedback to debug your command interface/call.
Also, as per this, it is inadvisable to provide the password on that command line. For that reason, the service/DB administrators probably disallow that form of accessing the service/DB/
This is how part of my shell script looks like.
#! /bin/sh
sftp -i $IdentityFile $ServerAddress << EOF
command 1 #Execute in the remote
command 2 #Execute in the remote
bye
EOF
command 3 #Execute locally
As per my current knowledge of scripting, if a command fails to execute, the control simply passes to the next command. But what if the sftp command fails to establish a network connection in the above block? Does it mean command 1 and command 2 will be executed locally? Or will the control jump to command 3?
How do I catch a possible error in sftp and direct the control to command 3? And if that is possible, can I detect the error using the ? variable, to take certain pre-emptive action? Some guidance will be great.
There are several things to do.
First, you need to extract the input as a function, to allow piping with command3, and it will be more legible:
function sfpInstruction() {
cat << EOF
command 1
command 2
bye
EOF
}
Thus, your sftp instruction can be changed to:
tmpFile="/tmp/errorFile.txt"
sftp -i $IdentityFile $ServerAddress $( sfpInstruction ) 2>"$tmpFile" || command3
Such a way:
all error messages are outpu in your error file
in any case, if sftp exits with a failing status (NOT 0), GNU/Bash will execute command3
if in addition you want command3 to read/check/parse the error messages, you can give it the "$tmpFile"
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 ....
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
I have a file to execute in Ksh written by someone. It has a set of commands to execute in sqlplus.
It starts with,
sqlplus -s $UP <<- END
followed by a set of ddl commands such as create,drop,etc.,
When I execute the file in the shell, I get the error in the starting line quoted above.
I understand "-s" starts the sqlplus in silent mode and $UP is the connection string with username/password. But I couldn't make heads or tails of "<<- END" part(Many sites from google says input redirection is "<<" not "<<-"). So I presumed the error must be in that part and removed it from the file.
Now it reads,
sqlplus -s $UP
But once I execute the file, It waits for input from the shell, instead of reading the rest of the lines from the file. How would I make sqlplus to execute the ddl commands in the rest of the file?. Thanks in advance.
Here "END" is a block marker and "-" is not required.
For running sqls from a shell script , One simple example is given below.
sqlplus system/manager << ENDOFSQL
whenever sqlerror exit sql.sqlcode;
select sysdate from dual;
exit;
ENDOFSQL
Thanks,
Rinson KE
DBA