Skip to Main Content

Database Software

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Support EXISTS in PL/SQL

Lukas EderSep 8 2017

I've encountered quite a few situations where I would have liked to write:

IF EXISTS (SELECT something...) THEN

  ...

ELSE

  ...

END IF;

Unfortunately, this is currently not possible. Instead, I have to do something like this:

DECLARE

  v NUMBER(1);

BEGIN

  SELECT CASE WHEN EXISTS (SELECT something...) THEN 1 ELSE 0 END

  INTO v

  FROM dual;

  IF v = 1 THEN

   ...

  ELSE

   ...

  END IF;

END;

Another workaround, if I don't need "ELSE" (although execution plans might be quite different from EXISTS variants):

FOR ignore IN (SELECT something... FETCH NEXT ROW ONLY) LOOP

...

END LOOP;

Of course, NOT EXISTS should be possible as well

Comments
Post Details
Added on Sep 8 2017
13 comments
1,739 views