Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script embedding Oracle PL/SQL code defining bind variable

If I run the below script, I am getting the error SP2-0552: Bind variable "OUTRES" not declared. So, how to define the bind variable OUTRES and where to define?

#!/usr/bin/bash
sqlplus -s scott/tiger << EOF 
declare ret varchar2(10):= '0';
begin
  begin
    insert into mytab(col1) values(1);
  exception
    when others then
      ret:=ret||'1';
  end;
  select ret into :OUTRES from dual;
end;
/
quit
EOF
like image 668
Ibrahim Quraish Avatar asked Jan 21 '14 07:01

Ibrahim Quraish


People also ask

How do you bind variables in Oracle PL SQL?

Use a bind variable in PL/SQL to access the variable from SQL*Plus. Bind variables are variables you create in SQL*Plus and then reference in PL/SQL. If you create a bind variable in SQL*Plus, you can use the variable as you would a declared variable in your PL/SQL subprogram and then access the variable from SQL*Plus.

How do you declare a bind variable in PL SQL?

You simply have to write a command which starts with keyword VARIABLE followed by the name of your bind variable which is completely user defined along with the data type and data width. That's how we declare a bind variable in Oracle database.

Can we use bind variables in Oracle stored procedure?

REFCURSOR bind variables can also be used to reference PL/SQL cursor variables in stored procedures. This allows you to store SELECT statements in the database and reference them from SQL*Plus. A REFCURSOR bind variable can also be returned from a stored function.

How do you call a PL SQL function from a shell script?

Show activity on this post. #!/bin/ksh CODE=`sqlplus -S $SCHEMA/$PW@//$IP_PORT/$DB << EOM Set timing on Set serveroutput on Whenever sqlerror exit failure; Whenever oserror exit failure; declare v_return number; begin PKG. Procedure(v_return); end; / EOM` if [ $? != 0 ] then echo "process failed." exit 1 fi exit $?


1 Answers

If you want to declare the bind variable in sqlplus. use the VAR keyword.

sqlplus -s scott/tiger << EOF 
VAR OUTRES NUMBER;
BEGIN
  NULL; /* Your Statements */
END;
/
EOF

You can also try quit :OUTRES and

quit :OUTRES
EOF
MYRESULT=$?
echo $MYRESULT

It output the return status in UNIX.

#!/usr/bin/bash
sqlplus -s scott/tiger << EOF 
VAR OUTRES NUMBER;
declare ret varchar2(10):= '0';
begin
  begin
    EXECUTE IMMEDIATE 'insert into mytab(col1) values(1)';
  exception
    when others then
      dbms_output.put_line(SQLERRM);
      ret:=ret||'1';
  end;
  :OUTRES := ret;
end;
/
quit :OUTRES
EOF
MYRESULT=$?
echo $MYRESULT
like image 116
Maheswaran Ravisankar Avatar answered Sep 22 '22 16:09

Maheswaran Ravisankar