Home | Previous Page | Next Page   Creating and Using SPL Routines > Defining and Using Variables >

Declaring Global Variables

A global variable has its value stored in memory and is available to other SPL routines, run by the same user session, on the same database. A global variable has the following characteristics:

Important:
You cannot define a collection variable as a global variable.

Figure 393 shows two SPL functions that share a global variable.

Figure 393.
CREATE FUNCTION func1()  RETURNING INT;
   DEFINE GLOBAL gvar INT DEFAULT 2;
   LET gvar = gvar + 1;
   RETURN gvar;
END FUNCTION;

CREATE FUNCTION func2()  RETURNING INT;
   DEFINE GLOBAL gvar INT DEFAULT 5;
   LET gvar = gvar + 1;
   RETURN gvar;
END FUNCTION;

Although you must define a global variable with a default value, the variable is only set to the default the first time you use it. If you execute the two functions in Figure 394 in the order given, the value of gvar would be 4.

Figure 394.
EXECUTE FUNCTION func1();
EXECUTE FUNCTION func2();

But if you execute the functions in the opposite order, as Figure 395 shows, the value of gvar would be 7.

Figure 395.
EXECUTE FUNCTION func2();
EXECUTE FUNCTION func1();

For more information, see Executing Routines.

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