Calling EDB Stored procedure(which is having output parameter) using EDB*plus from remote server - enterprisedb

EDB plus is being used for the purpose of calling Enterprise Database procedure (EDB SPL), the return value of the procedure is to be captured in a shell variable. Need syntax and details for the same. The database is on a remote machine.
I am able to call the EDB SPL (stored procedure) without mentioning it's output parameter using EDB * Plus from remote server. But not able to capture its output parameter's values into shell local variables.
--shell script code(OS is Linux, EDB version 11.3.10)
Proc_result=`edbplus -S $DB_USER/$DB_PASSWORD#$DB_SCHEMA 2>>EDB_ERROR_FILE <<END_OF_SQL
set feedback off
set pages 0
set flush off
set feedback on
exec schema1.procedure_1(input_param1, input_param2, input_param3);
exit;
END_OF_SQL`
--EDB procedure signature
schema1.procedure_1(input_param1 varchar2,
input_param2 number,
input_param3 varchar2,
output_param1 varchar2,
output_param2 number);
I want to get value of "output_param1" and "output_param2" in the shell variable "Proc_result"

Perhaps you need to use CALL instead of EXEC?
[root#ep11 bin]# DB_USER=enterprisedb
[root#ep11 bin]# DB_PASSWORD=abc123
[root#ep11 bin]# DB_SCHEMA="127.0.0.1:5432/edb"
[root#ep11 bin]# Proc_result=`edbplus -S $DB_USER/$DB_PASSWORD#$DB_SCHEMA 2>>EDB_ERROR_FILE <<END_OF_SQL
> set feedback off
> set pages 0
> set flush off
> set feedback on
> exec schema1.procedure_1('foo',1,'bar');
> exit;
> END_OF_SQL`
[root#ep11 bin]# echo $Proc_result;
EDB-SPL Procedure successfully completed.
[root#ep11 bin]# Proc_result=`edbplus -S $DB_USER/$DB_PASSWORD#$DB_SCHEMA 2>>EDB_ERROR_FILE <<END_OF_SQL
set feedback off
set pages 0
set flush off
set feedback on
call schema1.procedure_1('foo',1,'bar');
exit;
END_OF_SQL`
[root#ep11 bin]# echo $Proc_result;
02c6e1417aae6f6719a772fe7ea2cfac 1 CALL completed.
[root#ep11 bin]# psql -c "call schema1.procedure_1('foo',1,'bar')"
output_param1 | output_param2
----------------------------------+---------------
02c6e1417aae6f6719a772fe7ea2cfac | 1
(1 row)

Related

Midnight ghost within Taskscheduler

I am running into very strange behavior of scheduled code:
my code runs as expected by manual call within the cmd (it echos result = 1)
it still runs fine when I schedule it over the day
it does not run as expected at the night (it echos result = 0)
I set a variable in sqlplus and pass it into the Batch script errorlevel. then I echo the error level. I can only imagine that I overwrite my errorlevel at some point but I do not see why this should behave differently over the day.
batch script
#setlocal enableextensions Enabledelayedexpansion
#echo off
set logfile=test_log.log
set dbuser=some_guy#some_db
echo 1_start test... >> !logfile!
sqlplus !dbuser! test.sql >> !logfile!
echo CMD Return Value is: !errorlevel! >> !logfile!
echo 2_start test... >> !logfile!
sqlplus !dbuser! test.sql
echo CMD Return Value is: !errorlevel! >> !logfile!
endlocal
SQL File
variable return_value number
set serveroutput on
SET LINESIZE 10000
declare
null;
begin
:return_value := 1;
dbms_output.enable;
dbms_lock.sleep(1);
dbms_output.put_line('SQL return_value: ' || :return_value );
exception
when others then
:return_value := 5;
end;
/
set serveroutput off
exit :return_value
task-scheduler
run test.cmd at 3 AM and 11 AM
As mentioned above. I simply wanna see "CMD Return Value is: 1" somewhere in the log file. this does work fine, but not at night schedule. I did test some settings in the scheduler (e.g. configure for win-server 2012 or win 2007) - it did not change the result: At day time my return value is 1; at night it is 0.
I am using Windows Server 2012 and Oracle 19
Thank you for your help!

Shell Scripting - Anonymous Block

I have a procedure that does a certain task and exits.
The procedure has to be replicated to around 40 databases, that will run this procedure.
My concern is that if there is a change all across, every procedure has to be changed.
How can I create a shell script, that takes input
username, password and SID and runs an anonymous block (the same procedure is converted as an anonymous block and put on server)
and it runs it.
Here is the code I sometimes use:
dbs_list="
DB1.USR1
DB2.USR2
"
for x in ${dbs_list}
do
DB_=` echo ${x}|cut -f "1" -d . `
USR=` echo ${x}|cut -f "2" -d . `
echo "################################ #### "
echo "#processinb ${USR} # ${DB_} -- ...#"
echo "enter password:"
read -s PWD
sqlplus ${USR}/${PWD}#${DB_} << _EOF
set serveroutput on
prompt HERE is my anonymous code block
begin
dbms_output.put_line('do my things');
end;
/
_EOF
done
Note the importance:
to have this / thing at the end of the PLSQL code block
to have this last _EOF thing at the very beginning of the line after the sqlplus pseudo-file block (or so called famous Here-document )
Hope this helps.

Unable to pass in parameters to SQL Server Stored procedure from Windows batch script

I tried searching for the solution here but didn't find one that can solve my problem. I have following batch script:
for /f "tokens=1-3 delims=, " %%a in ('\path\batch_output.txt') do (
echo %%a, %%b, %%c
sqlcmd -S server -E -i path\spu_update_src_trg_ref.sql -v SourceName= %%a Instancname= %%b exitcode= %%c
ping 1.1.1.1 -n 1 -w 5000 > nul
)
Inside spu_update_src_trg_ref.sql I have below code:
use dbname
go
EXEC dbo.spu_update_src_trg_ref $(SourceName), $(Instancname), $(exitcode)
I am running the below batch script via a job scheduler so unable to see the direct error in the cmd. But my job is getting failed and the stored proc is also not getting executed. If need, stored proc is as below:
CREATE PROCEDURE dbo.spu_update_src_trg_ref
#SourceName VARCHAR(100),
#Instancname VARCHAR(100),
#exitcode INT
AS
BEGIN
IF #exitcode=0
BEGIN
UPDATE dbo.t_ctrm_ref_src_trg SET LoadStatus='Completed' WHERE SourceTableName=#SourceName;
UPDATE dbo.t_ctrm_instance_status SET InstanceStatus='Completed' WHERE InstanceName=#Instancname;
END
END
Its a simple sp that updates two tables, but I am unable to pass the input parameters from batch script. Please advice.
Update:
Thanks everyone for the help. I just removed some spaces and quotes('') from '\path\batch_output.txt' and it worked just fine. Appreciate all your help
There are syntax errors in your sqlcmd command. Remove the spaces between the var name, the equal sign, and the value in the "-v" portion.

How to capture the result of stored procedure through shell script?

I'm trying to execute stored procedure through shell script and try to get return from stored procedure but I didn't get any thing from the stored procedure on other hand same thing I do with sqlplus prompt and I'm able to get the result
sqlplus -silent xxx#xxx <<EOF
set serveroutput on
declare
DE_REC_COUNT number(10);
begin
DE_DUP_PROC ('T_MCL_30404_20150317_020','MCL','30404','FT',DE_REC_COUNT);
end;
EOF
Through sqlplus prompt
SQL> set serveroutput on
declare
DE_REC_COUNT number;
begin
DE_DUP_PROC ('T_MCL_30404_20150317_020','MCL','30404','FT',DE_REC_COUNT);
end;
0
PL/SQL procedure successfully completed.
The version of the anonymous block in the shell script will not be executed as shown, because you don't have a slash after the block to run it. If you run that you get no output at all. If you change it to have a slash:
sqlplus -silent xxx#xxx <<EOF
set serveroutput on
declare
DE_REC_COUNT number(10);
begin
DE_DUP_PROC ('T_MCL_30404_20150317_020','MCL','30404','FT',DE_REC_COUNT);
end;
/
EOF
then you'll see:
0
PL/SQL procedure successfully completed.
You've shown the interactive version in SQL*Plus without the slash too, but you must have had that to see the output you showed.
If you want the zero - which seems to be coming from a dbms_output call in your procedure, rather than directly from your anonymous block - n a shell variable you can refer to later, you can assign the output of the heredoc to a variable:
MY_VAR=`sqlplus -silent xxx#xxx <<EOF
set serveroutput on
set feedback off
declare
DE_REC_COUNT number(10);
begin
DE_DUP_PROC ('T_MCL_30404_20150317_020','MCL','30404','FT',DE_REC_COUNT);
end;
/
EOF`
printf "Got back MY_VAR as %s\n" ${MY_VAR}
Note that I've added set feedback off so you don't see the PL/SQL procedure successfully completed line. Now when you run that you'll see:
Got back MY_VAR as 0
and you can do whatever you need to with ${MY_VAR}. It depends what you mean by 'capture' though.
Here's an example of how it can be done by surrounding the code with the evaluation operators (` back quotes):
#!/bin/sh
results=`sqlplus -s xxx#xxx <<EOF
set serveroutput on feedback off
declare
DE_REC_COUNT number(10);
begin
DE_DUP_PROC ('T_MCL_30404_20150317_020','MCL','30404','FT',DE_REC_COUNT);
end;
/
EOF`
echo $results
Lets say for instance I have a below procedure which calculates the sum of two numbers and prints the total using dbms_output.put_line() method
CREATE OR REPLACE
PROCEDURE SUM
(
a IN NUMBER,
b IN NUMBER)
AS
total NUMBER;
BEGIN
total:= a + b;
dbms_output.put_line(total);
END;
Then in order to call this in our shell script we need to call this procedure in an anonymous block and store in a variable, later using echo we can execute the command.
plsqlcode=`sqlplus -silent hr/sandeep#orcl <<EOF
set serveroutput on
begin
sum(10,20);
end;
/
EOF`
echo $plsqlcode
Ideally this is not a recommended approach you should keep your shell and pl/sql code in separate files.

Call sql procedure through a shell script and email if error

I wish to write a shell script that calls a sql procedure(ultimately run the script through a scheduler) and if the procedure gives an sql error of any kind, send the error as an email which i wish to specify inside the script.
How can I do it?
EDIT: Here is what I tried. I cannot figure out how to put the logic that send email only if there is error.
export ORACLE_HOME=/oracle/app/products/11gr1/db
export ORACLE_SID=CTPP01S1
MAIL_TO="xxx#yyy.com"
LOGFILE=./xxx_job.log
exec 2>&1 > $LOGFILE
echo " Job run at: `date`......"
$ORACLE_HOME/bin/sqlplus -s '/ as sysdba' <<EOF
set head on;
set feed on;
set serveroutput on;
set linesize 250;
set pagesize 1000;
exec schema.proc_name;
exit
EOF
echo "Job ended at: `date`"
mailx -s "OLBB Extract Results." $MAIL_TO < $LOGFILE
If you want to send the mail from Oracle then utl_mail is your friend. As #Tony noted you didn't specify your platform, but in Linux there's mail and in Unix both of which you can pass the output of your execution to.
In any environment you can execute your SQL in a wrapper such as Python (I'm biased) and mail from that.