BEGIN

How to:

The BEGIN/ENDBEGIN construction enables you to issue a set of commands. Because you can use this construction anywhere an individual Maintain command can be used, you can use a set of commands where before you could issue only one command. For example, it can follow ON MATCH, ON NOMATCH, ON NEXT, ON NONEXT, or IF.


Top of page

x
Syntax: How to Use the BEGIN Command

The syntax for the BEGIN command is

BEGIN
   command 
   .
   .
   .
ENDBEGIN

where:

BEGIN

Specifies the start of a BEGIN/ENDBEGIN block.

Note: You cannot assign a label to a BEGIN/ENDBEGIN block of code or execute it outside the bounds of the BEGIN/ENDBEGIN construction in a procedure.

command

Is one or more Maintain commands, except for CASE, DECLARE, DESCRIBE, END, MAINTAIN, and MODULE. BEGIN blocks can be nested, allowing you to place BEGIN and ENDBEGIN commands between BEGIN and ENDBEGIN commands.

ENDBEGIN

Specifies the end of a BEGIN block.



Example: BEGIN With ON MATCH

The following example illustrates a block of code that executes when MATCH is successful:

MATCH Emp_ID
   ON MATCH BEGIN
      COMPUTE Curr_Sal = Curr_Sal * 1.05;
      UPDATE Curr_Sal;
      COMMIT;
      ENDBEGIN


Example: BEGIN With ON NEXT

This example shows BEGIN and ENDBEGIN with ON NEXT:

ON NEXT BEGIN
   TYPE "Next successful.";
   COMPUTE New_Sal = Curr_Sal * 1.05;
   PERFORM Cleanup;
   ENDBEGIN


Example: BEGIN With IF

You can also use BEGIN and ENDBEGIN with IF to run a set of commands depending on how an expression is evaluated. In the following example, BEGIN and ENDBEGIN are used with IF and FocError to run a series of commands when the prior command fails:

IF FocError NE 0 THEN BEGIN
   TYPE "There was a problem.";
   .
   .
   .
   ENDBEGIN


Example: Nested BEGIN Blocks

The following example nests two BEGIN blocks. The first one starts if there is a MATCH on Emp_ID and the second starts if UPDATE fails:

MATCH Emp_ID FROM Emps(Cnt);
ON MATCH BEGIN
   TYPE "Found employee ID <Emps(Cnt).Emp_ID";
   UPDATE Department Curr_Sal Curr_JobCode Ed_Hrs
      FROM Emps(Cnt);
   IF FocError GT 0 THEN BEGIN
      TYPE "Was not able to update the data source.";
      PERFORM Errorhnd;
      ENDBEGIN
   ENDBEGIN

WebFOCUS