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

Using an IF - ELIF - ELSE Structure

The SPL routine in Figure 408 uses an IF - ELIF - ELSE structure to compare the two arguments that the routine accepts.

Figure 408.
CREATE FUNCTION str_compare( str1 CHAR(20), str2 CHAR(20))
   RETURNING INTEGER;

   DEFINE result INTEGER;

   IF str1 > str2 THEN
       result = 1;
   ELIF str2 > str1 THEN
      result = -1;
   ELSE
      result = 0;
   END IF
   RETURN result;
END FUNCTION;

Suppose you define a table named manager with the columns that Figure 409 shows.

Figure 409.
CREATE TABLE manager
(
   mgr_name   VARCHAR(30),
   department VARCHAR(12),
   dept_no    SMALLINT,
   direct_reports SET( VARCHAR(30) NOT NULL ),
   projects LIST( ROW ( pro_name VARCHAR(15),
   pro_members SET( VARCHAR(20) NOT NULL ) )
                    NOT NULL),
   salary     INTEGER,
);

The SPL routine in Figure 410 uses an IF - ELIF - ELSE structure to check the number of elements in the SET in the direct_reports column and call various external routines based on the results.

Figure 410.
CREATE FUNCTION checklist( d SMALLINT )
   RETURNING VARCHAR(30), VARCHAR(12), INTEGER;

   DEFINE name VARCHAR(30);
   DEFINE dept VARCHAR(12);
   DEFINE num INTEGER;

   SELECT mgr_name, department,
          CARDINALITY(direct_reports) 
      FROM manager INTO name, dept, num
      WHERE dept_no = d;
   IF num > 20 THEN
      EXECUTE FUNCTION add_mgr(dept);
   ELIF num = 0 THEN
      EXECUTE FUNCTION del_mgr(dept);
   ELSE
      RETURN name, dept, num;
   END IF;

END FUNCTION;

The cardinality() function counts the number of elements that a collection contains. For more information, see Cardinality Function (IDS).

An IF - ELIF - ELSE structure in an SPL routine has up to the following four parts:

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