Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

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;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top