Sqlplus function output to bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sqlplus function output to bash
# 1  
Old 06-11-2014
Sqlplus function output to bash

Hi,
I would like to have the output from an Oracle procedure be captured into a bash variable, then emailed to me when it runs on the cron daily as such:
~~~~~bash script~~~~~~~~~~~
Code:
#!/bin/bash 
shellvar=`sqlplus -s <<EOF
execute test();
commit;
exit;
EOF`
echo $shellvar
mail -s "email title" myemail@test.com <<< "$shellvar rows inserted"

~~~~~~~~~~~~~~~~~

~~~~~oracle procedure~~~
Code:
create or replace procedure test (oRes OUT number) IS

begin

oRes:=1;

EXCEPTION
    WHEN OTHERS THEN
    raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
end;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This doesn't return anything.

The only thing that works is using a function instead of a procedure and then using select as in below.

~~~~~~bash script~~~~~~~~~~
Code:
#!/bin/bash
shellvar=`sqlplus -s <<EOF
select test() from dual;
commit;
exit;
EOF`
echo $shellvar
mail -s "email title" myemail@test.com <<< "$shellvar rows inserted"

~~~~~~~~~~~~~~~~~

~~~~~~~~Oracle function~~~~~~~~~~~~~~~~~~~~~
Code:
create or replace function test return number IS

oRes number;
begin

select 1 into oRes from dual;

return oRes;
EXCEPTION
    WHEN OTHERS THEN
    raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
end;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data

Last edited by vbe; 06-11-2014 at 05:16 PM.. Reason: removed extra blak spaces to fit the screen...
# 2  
Old 06-11-2014
Why do you need a shell variable?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing string from bash to sqlplus

Hello, I have file (PARFILE) with string on first line: INCLUDE=SCHEMA:"IN\( 'SCHEMA1','SCHEMA2','SCHEMA3' \)"In .sh script I use: .... IMPORT_SCHEMA=`awk 'NR==1{print $2}' ${PARFILE}` ...print $2 is because 'SCHEMA1','SCHEMA2','SCHEMA3' is 2nd column in file echo "$IMPORT_SCHEMA"... (5 Replies)
Discussion started by: DjukaZg
5 Replies

2. Shell Programming and Scripting

Passing Oracle function as file input to sqlplus

Apologies if this is the incorrect forum.There is an issue in the function call I am facing while calling the same from a unix shell scripts. Basically, I want the ref cursor to return values to a variable in sqlpus. The function call is currently saved in a ".txt" file in a unix location. I want... (7 Replies)
Discussion started by: amvip
7 Replies

3. Solaris

sqlplus output from ksh.

Hi All, I have the below simple script. It runs just fine by itself when I manually invoke it. But once I put it in the crontab with entry: * * * * * /users/myuser/test.ksh >> /users/myuser/log/test.txt" It does NOT print the returned value ($REMAIN) from the DB!? The result in the... (3 Replies)
Discussion started by: steve701
3 Replies

4. Shell Programming and Scripting

Log sqlplus output from Shell

UNIX Gods, I'll be running this script from CRON. I need to log the status of each of the six sqlplus calls into a file when this job is kicked off. Any suggestions? Thanks in advance. #!/bin/ksh export USAGE="USAGE: `basename $0` -e <DBUSER> <DBPASSWD> <TNSNAME>" if ; then ... (2 Replies)
Discussion started by: WhoDatWhoDer
2 Replies

5. Shell Programming and Scripting

store sqlplus output in variable

hi how can i store sqlplus output to a variable in sh script (not bash) Thanks MM (1 Reply)
Discussion started by: murtymvvs
1 Replies

6. Shell Programming and Scripting

In bash getting weird output from function ?

My script- result="" times() { echo "inside the times function" result=8 echo "Inside function $result" return $result } result=$(times) echo "the value is "$? echo "the value of result $result" when I run I get this, why the value still remain 0. $ ./func the value is 0 the value... (5 Replies)
Discussion started by: boy18nj
5 Replies

7. Shell Programming and Scripting

Formatting Oracle sqlplus output

a job extracts orcle data into unix as flat file. a single record breaks into two record in unix flat file. This is the case only for 6 records out of 60 lack records. (its not single record in two line. but its single record into record. ie., \n come into picture) can you tell me what... (6 Replies)
Discussion started by: Gopal_Engg
6 Replies

8. Shell Programming and Scripting

output arguments from a sqlplus

Hi. I need to output a 4 queries result into another application using result=`sqlplus -s ${3}/${4}@${2} << EOF ... query1 query2 query3 query4 .... echo "$metrics1" and returning those individual values into another app. (query1 and 3compute one value, query 2 and 4 compute 4... (3 Replies)
Discussion started by: shell_zen
3 Replies

9. UNIX for Dummies Questions & Answers

Getting output parameter in sqlplus

I need to get the output parameter from a stored procedure in sql plus using shell script. Can anyone help me please ... (1 Reply)
Discussion started by: risshanth
1 Replies

10. UNIX for Advanced & Expert Users

sqlPlus output with new line character

Hi, I have a script which calls a sqlplus command and i'm saving the output in a variable. Z=`sqlplus -s $TC_ORACLE_USER/$TC_ORACLE_PASSWORD@$TC_CONNECT_STRING <<eof set echo off set head off set serveroutput on; set feedback off; select description from period where trunc(sysdate)... (2 Replies)
Discussion started by: decci_7
2 Replies
Login or Register to Ask a Question