5

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

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the reply, but Mahesh could you tell me where exactly in the above code, I should set the VAR (bind variable)?
By the way, if there is any error occurred in the PL/SQL code block for eg, invalid table name or column name, the error value always returned is 1 in spite of changing the ret to store some other value other than the 1.
you should declare before you run your pl/sql block, and after entering into sqlplus.
And, the PL/SQL will end up with compilation error, when the TABLE name is invalid. I edited my answer to do it using dynamic SQL.
0
#!/usr/bin/bash
sqlplus -s scott/tiger << EOF 
declare 
ret varchar2(10):= '0';
OUTRES  varchar2(10);
begin
  begin
    insert into mytab(col1) values(1);
  exception
    when others then
      ret:=ret||'1';
  end;
  select ret into OUTRES from dual;
  dbms_output.put_line('Value of OUTRES is' || OUTRES);
end;
/
quit
EOF

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.