Home | Previous Page | Next Page   Creating and Using SPL Routines > Writing the Statement Block >

Implicit and Explicit Statement Blocks

In an SPL routine, the implicit statement block extends from the end of the CREATE statement to the beginning of the END statement. You can also define an explicit statement block, which starts with a BEGIN statement and ends with an END statement, as Figure 402 shows.

Figure 402.
BEGIN
   DEFINE distance INT;
   LET distance = 2;
END

The explicit statement block allows you to define variables or processing that are valid only within the statement block. For example, you can define or redefine variables, or handle exceptions differently, for just the scope of the explicit statement block.

The SPL function in Figure 403 has an explicit statement block that redefines a variable defined in the implicit block.

Figure 403.
CREATE FUNCTION block_demo()
   RETURNING INT;
      DEFINE distance INT;
   LET distance = 37;
   BEGIN
      DEFINE distance INT;
      LET distance = 2;
   END
   RETURN distance;

END FUNCTION;

In this example, the implicit statement block defines the variable distance and gives it a value of 37. The explicit statement block defines a different variable named distance and gives it a value of 2. However, the RETURN statement returns the value stored in the first distance variable, or 37.

Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]