Running pl/sql in Korn Shell(AIX) - oracle

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

Related

Run sqlplus commands from multiple files with query logging or spool in Windows Batch File

I am quite new to batch scripting, and I am trying to run multiple sql files, which in turn may contain multiple sql DML/DDL queries from bat file. The output files must contain all the queries being executed and the query output. Unlike this example , I don't have spool command inside my sql file, and I can not edit the input sql files. The following command works for me in ksh file (thanks to here-document):
$sqlplus /nolog <<! >>$sqlLogs.lst
connect $USERNAME/$PASSWORD#${DBNAME}
set echo on timing on
spool ${SCRIPTRUNFILE_SPOOL}
select name from v\$database;
#${SCRIPTRUNFILE};
spool off
exit
!
I want the exact same in Windows bat file. I have tried using ^. I can't do combine all sql files into one, as I need logging for each sql file into different file. My attempt at the bat file script is as follows and I have played around this much, and it fails with spool command not recognized. I also prefixed below commands with sqlplus, but still unable to achieve something like above ksh file:
sqlplus -s username/pwd#DBName >> sqlLogs.lst
set echo on timing on
spool %RUNFILENAME%.lst
#%RUNFILENAME% > %RUNFILENAME%.lst
select name from v\$database;
spool off
quit
Following logic executes my scripts but does not log the query being executed. Also, I don't want to connect twice to the database.
echo select name from v$database; | sqlplus -s username/pwd#DBName >> sqlLogs.lst
echo quit | sqlplus -s username/pwd#DBName #%SCRIPTRUNFILE%>> %SCRIPTRUNFILE_SPOOL%.lst
Can someone here please help to spool to a file where I can log the queries as well, while maintaining a single DB Connection?
Pass your immediate SQL*Plus commands to your sqlplus via stdout, grouped together by Windows' ( and ) symbols...
(
echo.set echo on timing on
echo.spool %SCRIPTRUNFILE_SPOOL%
echo.select name from v$database;
echo.#%SCRIPTRUNFILE%
echo.spool off
echo.exit
) | sqlplus -s %USERNAME%/%PASSWORD%#%DBNAME% >> sqlLogs.lst

sqlplus not properly ended script

i have a plsql script, which is not properly terminated:
create or replace package mypackage
... (no semicolon/forward slash at the end)
the script is executed with sqlplus (12.1) on windows powershell:
sqlplus user/pass#host #my_not_properly_ended_file.pks
i would expect sqlplus to terminate with exit code 1 and an error message, but instead it prompts for input.
how can i get an error message and exit code in this situation?
edit: solution should also work with dml statements that are not terminated with a semicolon.
You can use shell redirection instead of #:
sqlplus user/pass#host < my_not_properly_ended_file.pks
This will also prevent it getting 'stuck' if the script doesn't end with an exit command.
However, it won't return an error code to the shell in either case. As far as SQL*Plus is concerned you put the incomplete statement into its buffer but never attempted to execute it (as there was no slash); and as it didn't run, it didn't error. So setting whenever sqlerror or whatever won't make any difference either.

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.

Using SQLPLUS command in UNIX scripts

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

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 ....

Resources