Skip to content

Instantly share code, notes, and snippets.

@velll
Created October 27, 2014 09:44
Show Gist options
  • Select an option

  • Save velll/08a4a1cf3c0ab7097da1 to your computer and use it in GitHub Desktop.

Select an option

Save velll/08a4a1cf3c0ab7097da1 to your computer and use it in GitHub Desktop.
PL/SQL short-cut evaluation
-- Coalesce has short-cut evaluation so it does not evaluate second parameter if the
-- first one is ok
DECLARE
num_Result NUMBER;
num_Something NUMBER := 1;
--
FUNCTION RAISES
RETURN NUMBER
IS
BEGIN
RAISE_APPLICATION_ERROR(-20100, 'Here be dragons');
RETURN 0;
END RAISES;
BEGIN
num_Result := COALESCE(num_Something, RAISES);
RAISE_APPLICATION_ERROR(-20101, num_Result);
END;
/
-- If it is not, wll, then everything goes as expected
DECLARE
num_Result NUMBER;
num_Something NUMBER;
--
FUNCTION RAISES
RETURN NUMBER
IS
BEGIN
RAISE_APPLICATION_ERROR(-20100, 'Here be dragons');
RETURN 0;
END RAISES;
BEGIN
num_Result := COALESCE(num_Something, RAISES);
RAISE_APPLICATION_ERROR(-20101, num_Result);
END;
/
-- NVL has no short-cut evaluation
DECLARE
num_Result NUMBER;
num_Something NUMBER;
--
FUNCTION RAISES
RETURN NUMBER
IS
BEGIN
RAISE_APPLICATION_ERROR(-20100, 'Here be dragons');
RETURN 0;
END RAISES;
BEGIN
num_Result := NVL(num_Something, RAISES);
RAISE_APPLICATION_ERROR(-20101, num_Result);
END;
/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment