Select clausule inside pl/sql function return wron

2020-05-09 19:48发布

问题:

When I do this:

select sum(m.mot)
from rmtq mq
join rmo m on mq.id = m.id
where mq.another = 138;

return value = 2, which is correct. But when I put this code inside a function:

create or replace function get(another in number) return number
   is ret number := 0;
   begin
      select sum(m.mot)
              into ret
              from rmtq mq
              join rmo m on mq.id = m.id
              where mq.another = another
       return(ret);
    end;

and I call:

exec dbms_output.put_line(get(138));

return value = 39, which is incorrect. What is that 39?

回答1:

The comments were right to question; lest anyone waste time on this you have to write the names of the parameters correctly, for example:

create or replace function get(p_another in number) return number
   is ret number := 0;
   begin
      select sum(m.mot)
              into ret
              from rmtq mq
              join rmo m on mq.id = m.id
              where mq.another = p_another
       return(ret);
    end;