Link to home
Start Free TrialLog in
Avatar of yajesh
yajeshFlag for United States of America

asked on

BEGIN....END within IF......END IF Construct

Can you have several BEGIN.....END  constructs within a single IF.....END IF? Would it be wise to insert an EXCEPTION Handler within each BEGIN.....END.

For Example:
IF........................ THEN -- Start
BEGIN
     NULL;
END;

BEGIN
    NULL;
END;

BEGIN
      NULL;
END;


END IF; -- End

Yaj
Avatar of Milleniumaire
Milleniumaire
Flag of United Kingdom of Great Britain and Northern Ireland image

Your syntax is perfectly valid.

Why do you need to have multiple blocks?  Usually, this is necessary to code for specific exceptions.  Is this the case for you?
Avatar of knightEknight
You would need to surround all of the inner block in it's own BEGIN - IF like this:


IF ...
BEGIN

  BEGIN
     NULL;
  END;

  BEGIN
    NULL;
  END;

  BEGIN
      NULL;
  END;

END IF;
knightEknight, it isn't necessary to surround the individual blocks with a BEGIN and END.  The syntax supplied by vajesh will work.
Avatar of yajesh

ASKER

A very valid question, Milleniumaire.
First of all I am newbie to PL/SQL, still learning tricks of the trade.

In my IF. THEN...............END IF construct, I have few SELECT followed by UPDATE statements. Say for example I have 3 groups of SELECT....INTO respective variables with its own WHERE criteria followed by UPDATE another TABLE based on SELECT statement.

So my code would look something like this:
IF ................ THEN
  SELECT.................

  UPDATE

  SELECT...................

  UPDATE

  etc..
etc..

END IF;

So I was thinking if I can use BEGIN....END within each SELECT....UPDATE group, then I mighty be able to insert ECEPTION Handles for each group withion each BEGIN.... END, instead of single EXCEPTION Handler before END IF.


yaj
 
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

What you mention is the exact reason to have the ability to nest PL/SQL blocks:  The ability to trap and handle exceptions with a local scope.
ASKER CERTIFIED SOLUTION
Avatar of Milleniumaire
Milleniumaire
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yajesh

ASKER

Thanks, Milleniumaire,
I understand it better now.

Further to that, I was also able to find additional details, that is also most probably in line with your suggestion too.

http://www.java2s.com/Tutorial/Oracle/0480__PL-SQL-Programming/AcompleteexampleusingRAISEAPPLICATIONERROR.htm

yaj