Server-side Variables

OpenBase provides server-side global variables to make it easy to coordinate between stored procedures and database clients. The variables are thread-safe and can be safely incremented and decremented to provide specialized key generation or state management.

Variables are only stored in memory, so it is recommended that you create a stored procedure to initialize server-side variables on startup of the server.

If you have a mirrored/clustered environment, please note that the variable declaration is not mirrored. This is intentional since some sites may want to have site-specific values. If you need the values mirrored, consider using a table for storing the values.

Declaring Global Variables

You can declare a server variable by executing this syntax as SQL or declaring the variable in OpenScript.

DECLARE GLOBAL <variable name> AS <type> INITIALIZE <value>

Here is an example:

DECLARE GLOBAL invoice_number AS INTEGER INITIALIZE 1000

Possible variable types include: STRING, INTEGER, MONEY and FLOAT.

Incrementing and Decrementing Global Variables

If you are using a numeric variable type, you can use INCREMENT and DECREMENT to modify the value in a thread-safe manner.

By thread-safe we mean that if 10 processes all ask the server to increment the value at the same time, they will all take turns so two clients don’t generate the same number.

INCREMENT invoice_number

Increment increases the value of the variable by 1 and returns the current value.

DECREMENT invoice_number

Decrement decreases the value of the variable by 1 and returns the current value.

Setting a Variable Value

You can use this syntax to set the value of a server variable.

SET <variable name> TO <value>

Currently we only support a simple value. Here is an example:

SET invoice_number TO 2000

When you set the value of a global variable in OpenScript the value is actually stored on the server.

Getting Variable Values

Sometimes you may want to fetch the value of a variable. You can do this using the PRINT VARIABLE command. Here is an example:

PRINT VARIABLE invoice_number

This syntax is specific to the database server and cannot be used inside OpenScript. With OpenScript, use the global variables as you normally would and the values will flow through from the database server.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License