PL/SQL How to call a function without getting returned object
Asked Answered
L

3

8

I have a function in PL/SQL:

FUNCTION do_something
  RETURN BOOLEAN
IS
  ...
BEGIN
  ...
END

This function can be called as such:

DECLARE
  answer BOOLEAN;
BEGIN
  answer := do_something();
END

Now, there are cases, where I don't need the returned boolean. If I don't want to get it and just write:

do_something();

I get PLS-00306 (wrong number of types of arguments in call to DO_SOMETHING) Is there a way to call it without declaring and setting a new boolean, which I will never use in this case?

Laroche answered 10/2, 2014 at 12:27 Comment(5)
Strange... I'd expect a PLS-002221: 'do_something' is not a procedure or undefined maybe because of the empty (). Anyway, not that I recomend it but you can put it in a if statement i.e. IF(do_something) then null; end if; but this is really uglyCallimachus
Kind of strange as the point of a function is to return something, otherwise it should probably be a procedure.Philipp
@tbone, agreed; the function should be called something like something_was_done() :)Tientiena
It's a valid question for java, c or pascal developers. Sometimes you really don't need return value and it makes code ugly. here's example: user_node := xmldom.appendChild(bund_node, xmldom.makeNode(item_elmt)); so after you append child to the parent object, you don't need a reference to that child.Pilcomayo
Equally ridiculous as @Callimachus version. for x in (select do_something() from dual) loop exit; end loop;. I'm starting to really hate plsql.Speciality
I
12

Very simple: create a procedure which covers this function

PROCEDURE do_something
IS
  dummy boolean;
BEGIN
  dummy := do_something();
END;

Sorry, but this is the only way in PL/SQL. This language is very strict in definitions of procedure and function and you cannot make a function call without handling the result. But you can make a procedure as it is shown in example above.

It will define automatically where to choose the function and where the procedure.

EDIT

As far as there are people who do not trust me (sometimes I really tell bad things so doubts are allowed :) ) this is the test:

declare
  myresult boolean;

  function do_something return boolean
    is
  begin
    return true;
  end;

  procedure do_something
    is
      dummy boolean;
  begin
    dummy := do_something();
  end;
begin
  myresult := do_something();

  do_something();
end;

works well.

Iamb answered 10/2, 2014 at 12:46 Comment(3)
@Simn, Wouldn't it give a ORA-00955: name is already used by an existing object ?Callimachus
No... that's because you defined it within a an anonymous block. Try to do the same in schema levelCallimachus
I think the schema-level example does not work because you cannot create even a package with a name of existing table. So it is done a bit differently inside of the code to support polymorphism.Iamb
C
1

Here is a useful alternative to assigning a dummy variable:

DECLARE
  FUNCTION f_yes RETURN boolean IS
  BEGIN
    return true;
  END f_yes;
BEGIN
  IF f_yes THEN
    null;
  END IF;
END;

This is slightly more helpful when there are functions of different return types you want to call without using their results. It's also helpful for writing empty IF statements, which could be useful to to simplify code by eliminating possibilities for the real condition you want to match in a later ELSIF.

Courtmartial answered 29/4, 2022 at 15:20 Comment(0)
S
1

I'm in the same situation and the shortest solution I've come up is wrapping it in an IF statement:

if do_something then
  null;
end if;
Snailpaced answered 25/5, 2022 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.