Application Development Guide


Host Variables in REXX

Host variables are REXX language variables that are referenced within SQL statements. They allow an application to pass input data to DB2 and receive output data from DB2. REXX applications do not need to declare host variables, except for LOB locators and LOB file reference variables. Host variable data types and sizes are determined at run time when the variables are referenced. Apply the following rules when naming and using host variables.

Naming Host Variables in REXX

Any properly named REXX variable can be used as a host variable. A variable name can be up to 64 characters long. Do not end the name with a period. A host variable name can consist of alphabetic characters, numerics, and the characters @, _, !, ., ?, and $.

Referencing Host Variables in REXX

The REXX interpreter examines every string without quotation marks in a procedure. If the string represents a variable in the current REXX variable pool, REXX replaces the string with the current value. The following is an example of how you can reference a host variable in REXX:

     CALL SQLEXEC 'FETCH C1 INTO :cm' 
     SAY 'Commission = ' cm 

To ensure that a character string is not converted to a numeric data type, enclose the string with single quotation marks as in the following example:

     VAR = '100' 

REXX sets the variable VAR to the 3-byte character string 100. If single quotation marks are to be included as part of the string, follow this example:

     VAR = "'100'" 

When inserting numeric data into a CHARACTER field, the REXX interpreter treats numeric data as integer data, thus you must concatenate numeric strings explicitly and surround them with single quotation marks.

Indicator Variables in REXX

An indicator variable data type in REXX is a number without a decimal point. Following is an example of an indicator variable in REXX using the INDICATOR keyword.

     CALL SQLEXEC 'FETCH C1 INTO :cm INDICATOR :cmind' 
     IF ( cmind < 0 ) 
        SAY 'Commission is NULL' 

In the above example, cmind is examined for a negative value. If it is not negative, the application can use the returned value of cm. If it is negative, the fetched value is NULL and cm should not be used. The database manager does not change the value of the host variable in this case.

Predefined REXX Variables

SQLEXEC, SQLDBS and SQLDB2 set predefined REXX variables as a result of certain operations. These variables are:

RESULT
Each operation sets this return code. Possible values are:
n
Where n is a positive value indicating the number of bytes in a formatted message. The GET ERROR MESSAGE API alone returns this value.
0
The API was executed. The REXX variable SQLCA contains the completion status of the API. If SQLCA.SQLCODE is not zero, SQLMSG contains the text message associated with that value.
-1
There is not enough memory available to complete the API. The requested message was not returned.
-2
SQLCA.SQLCODE is set to 0. No message was returned.
-3
SQLCA.SQLCODE contained an invalid SQLCODE. No message was returned.
-6
The SQLCA REXX variable could not be built. This indicates that there was not enough memory available or the REXX variable pool was unavailable for some reason.
-7
The SQLMSG REXX variable could not be built. This indicates that there was not enough memory available or the REXX variable pool was unavailable for some reason.
-8
The SQLCA.SQLCODE REXX variable could not be fetched from the REXX variable pool.
-9
The SQLCA.SQLCODE REXX variable was truncated during the fetch. The maximum length for this variable is 5 bytes.
-10
The SQLCA.SQLCODE REXX variable could not be converted from ASCII to a valid long integer.
-11
The SQLCA.SQLERRML REXX variable could not be fetched from the REXX variable pool.
-12
The SQLCA.SQLERRML REXX variable was truncated during the fetch. The maximum length for this variable is 2 bytes.
-13
The SQLCA.SQLERRML REXX variable could not be converted from ASCII to a valid short integer.
-14
The SQLCA.SQLERRMC REXX variable could not be fetched from the REXX variable pool.
-15
The SQLCA.SQLERRMC REXX variable was truncated during the fetch. The maximum length for this variable is 70 bytes.
-16
The REXX variable specified for the error text could not be set.
-17
The SQLCA.SQLSTATE REXX variable could not be fetched from the REXX variable pool.
-18
The SQLCA.SQLSTATE REXX variable was truncated during the fetch. The maximum length for this variable is 2 bytes.

Note:The values -8 through -18 are returned only by the GET ERROR MESSAGE API.

SQLMSG
If SQLCA.SQLCODE is not 0, this variable contains the text message associated with the error code.

SQLISL
The isolation level. Possible values are:
RR
Repeatable read.
RS
Read stability.
CS
Cursor stability. This is the default.
UR
Uncommitted read.
NC
No commit (NC is only supported by some host or AS/400 servers.)

SQLCA
The SQLCA structure updated after SQL statements are processed and DB2 APIs are called. The entries of this structure are described in the Administrative API Reference.

SQLRODA
The input/output SQLDA structure for stored procedures invoked using the CALL statement. It is also the output SQLDA structure for stored procedures invoked using the Database Application Remote Interface (DARI) API. The entries of this structure are described in the Administrative API Reference.

SQLRIDA
The input SQLDA structure for stored procedures invoked using the Database Application Remote Interface (DARI) API. The entries of this structure are described in the Administrative API Reference.

SQLRDAT
An SQLCHAR structure for server procedures invoked using the Database Application Remote Interface (DARI) API. The entries of this structure are described in the Administrative API Reference.

LOB Host Variables in REXX

When you fetch a LOB column into a REXX host variable, it will be stored as a simple (that is, uncounted) string. This is handled in the same manner as all character-based SQL types (such as CHAR, VARCHAR, GRAPHIC, LONG, and so on). On input, if the size of the contents of your host variable is larger than 32K, or if it meets other criteria set out below, it will be assigned the appropriate LOB type.

In REXX SQL, LOB types are determined from the string content of your host variable as follows:
Host variable string content Resulting LOB type

:hv1='ordinary quoted string longer than 32K ...'

CLOB

:hv2="'string with embedded delimiting quotation marks ",
"longer than 32K...'"

CLOB

:hv3="G'DBCS string with embedded delimiting single ",
"quotation marks, beginning with G, longer than 32K...'"

DBCLOB

:hv4="BIN'string with embedded delimiting single ",
"quotation marks, beginning with BIN, any length...'"

BLOB

LOB Locator Declarations in REXX

Syntax for LOB Locator Host Variables in REXX shows the syntax for declaring LOB locator host variables in REXX.

Syntax for LOB Locator Host Variables in REXX
 
            .-,-------------------.
            V                     |
>>-DECLARE-----:--variable-name---+---LANGUAGE TYPE--+-BLOB---+---LOCATOR-->
                                                     +-CLOB---+
                                                     '-DBCLOB-'
 
>--------------------------------------------------------------><
 

You must declare LOB locator host variables in your application. When REXX/SQL encounters these declarations, it treats the declared host variables as locators for the remainder of the program. Locator values are stored in REXX variables in an internal format.

Example:

     CALL SQLEXEC 'DECLARE :hv1, :hv2 LANGUAGE TYPE CLOB LOCATOR' 

Data represented by LOB locators returned from the engine can be freed in REXX/SQL using the FREE LOCATOR statement which has the following format:

Syntax for FREE LOCATOR Statement
 
                  .-,-------------------.
                  V                     |
>>-FREE--LOCATOR-----:--variable-name---+----------------------><
 

Example:

     CALL SQLEXEC 'FREE LOCATOR :hv1, :hv2'

LOB File Reference Declarations in REXX

You must declare LOB file reference host variables in your application. When REXX/SQL encounters these declarations, it treats the declared host variables as LOB file references for the remainder of the program.

Syntax for LOB File Reference Variables in REXX shows the syntax for declaring LOB file reference host variables in REXX.

REXX File Reference Declarations
 
            .-,-------------------.
            V                     |
>>-DECLARE-----:--variable-name---+---LANGUAGE TYPE--+-BLOB---+---FILE-->
                                                     +-CLOB---+
                                                     '-DBCLOB-'
 
>--------------------------------------------------------------><
 

Example:

     CALL SQLEXEC 'DECLARE :hv3, :hv4 LANGUAGE TYPE CLOB FILE' 

File reference variables in REXX contain three fields. For the above example they are:

hv3.FILE_OPTIONS.
Set by the application to indicate how the file will be used.
hv3.DATA_LENGTH.
Set by DB2 to indicate the size of the file.
hv3.NAME.
Set by the application to the name of the LOB file.

For FILE_OPTIONS, the application sets the following keywords:

Keyword (Integer Value)
Meaning
READ (2)
File is to be used for input. This is a regular file that can be opened, read and closed. The length of the data in the file (in bytes) is computed (by the application requestor code) upon opening the file.
CREATE (8)
On output, create a new file. If the file already exists, it is an error. The length (in bytes) of the file is returned in the DATA_LENGTH field of the file reference variable structure.
OVERWRITE (16)
On output, the existing file is overwritten if it exists, otherwise a new file is created. The length (in bytes) of the file is returned in the DATA_LENGTH field of the file reference variable structure.
APPEND (32)
The output is appended to the file if it exists, otherwise a new file is created. The length (in bytes) of the data that was added to the file (not the total file length) is returned in the DATA_LENGTH field of the file reference variable structure.
Note:A file reference host variable is a compound variable in REXX, thus you must set values for the NAME, NAME_LENGTH and FILE_OPTIONS fields in addition to declaring them.

Clearing LOB Host Variables in REXX

On OS/2 it may be necessary to explicitly clear REXX SQL LOB locator and file reference host variable declarations as they remain in effect after your application program ends. This is because the application process does not exit until the session in which it is run is closed. If REXX SQL LOB declarations are not cleared, they may interfere with other applications that are running in the same session after a LOB application has been executed.

The syntax to clear the declaration is:

     CALL SQLEXEC "CLEAR SQL VARIABLE DECLARATIONS" 

You should code this statement at the end of LOB applications. Note that you can code it anywhere as a precautionary measure to clear declarations which might have been left by previous applications (for example, at the beginning of a REXX SQL application).


[ Top of Page | Previous Page | Next Page ]