Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle DB: how to store function result into variable inside procedure

Good day. I have a function:

create function get_n(search tt.pp%type)
  return number
  is rc number;
begin
  select count(*)
  into rc
  from tt
  where tt.pp=search;

  return (rc);
end;
/

and i can get result as

variable qwe number;

begin
  select get_n('sample')
    into :qwe
    from dual;
end;

print qwe;

So, it's successfully works. But by parts: i can't exec line with print at execution of other (PLS-00103: Encountered the symbol "PRINT"...). And it's really strange.

I try to get result from function in anonymous block and print it:

declare
  qwe number;
begin
  select get_n('sample')
    into :qwe
    from dual;
  dbms_output.put_line(qwe);
exception
  when others
    then dbms_output.put_line(sqlerrm);
end;
/

And it's not print anything. Why?

like image 792
Evgeniy175 Avatar asked Dec 19 '25 23:12

Evgeniy175


1 Answers

Problem is :. Following code should work:

declare
  qwe number;
begin
  select get_n('sample')
    into qwe
    from dual;
  dbms_output.put_line(qwe);
exception
  when others
    then dbms_output.put_line(sqlerrm);
end;
/

: means variable that need to be binded not variable inside PL/SQL block.
And in case of first block you're missing / after PL/SQL block what causes compiler reads print as part of PL/SQL not SQLplus script:

variable qwe number;

begin
  select get_n('sample')
    into :qwe
    from dual;
end;
/

print qwe;
like image 180
Kacper Avatar answered Dec 22 '25 21:12

Kacper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!