REXX is an extremely versatile language. It offers efficient programming structures, powerful functions and extensive mathematical capabilities. Commands to host environments can be freely intermixed with REXX statements. This makes the language particularly suitable for command procedures, application prototyping or TCP/IP connectivity.
REXXSQL, a product developed by Software Product Research, provides an SQL interface for REXX/VM EXECs.
REXXSQL is implemented as an external REXX routine, invoked using the call "REXXSQL input_argument ".
The input argument is a character string, a REXX variable or expression. It contains or refers to the SQL statement to be executed. After submitting the statement to DB2, REXXSQL returns the completion status and the execution results to the invoking procedure as REXX variables or stems.
The completion status includes:
All SQL statements valid in an application program can be submitted to REXXSQL. This includes CONNECT, SELECT, UPDATE, DELETE, INSERT, COMMIT and DDL statements like CREATE or DROP. In dynamic mode, these SQL statements may be issued against any DB2 platform that can be reached from DB2/VM-VSE. Static REXXSQL requests can be used against DB2/VM-VSE databases only, as the "extended dynamic execution" mode implied is unknown in DB2 platforms other than DB2/VM-VSE.
Submitting dynamic SQL statements
The dynamic REXXSQL mode is the simplest interface to DB2. Input to the call is a character string or a REXX expression that contains the SQL statement to be executed. If a SELECT is submitted, the fetched columns are returned in REXX stems. Execution status is returned for all statements, as described above.
REXXSQL issues an SQL "prepare" for the submitted text. A SELECT statement is processed using an OPEN / FETCH / CLOSE sequence on a dynamic cursor. Non-SELECT statements are processed using an EXECUTE IMMEDIATE call.
Dynamic FETCH interface
A SELECT statement that returns a large number of rows may need considerable amounts of storage for the column stems. To avoid storage problems, a FETCH interface has been designed to select one table row at a time. The interface is opened with a "REXXSQL OPEN" call. Each "REXXSQL FETCH" call transfers a single table row. A "REXXSQL CLOSE" call terminates the fetch sequence.
While the dynamic interface is easy to use, it incurs the overhead of DB2 "prepare" processing. Since this overhead is not trivial, SQL statements that are executed often can be prepped and executed in the "static" mode.
Prepping SQL statementsA REXXSQL procedure can create a DB2 package, containing multiple SQL statements (package sections). Package creation is initiated using a REXXSQL CREATE PACKAGE call.
Each SQL statement is added to the new package by means of a "REXXSQL PREPARE" call. The statement is identified by a name that will be used as a reference to the statement, when executing the package. REXXSQL uses a package control table to store the relationship between the statement name and the corresponding package section number, assigned by DB2.
If the prepped statement contains variables, the following applies:
A hostvariable definition starts with
a semicolon, followed by the datatype and length of the hostvariable, for
INSERT INTO <table> VALUES ( :INTEGER , :CHAR( 8) ).
Since parameter markers do not specify the format of the hostvariable at prep time, an implicit definition must take place during execution, depending on the actual contents of the variables.
Executing prepped SQL statements
The "REXXSQL EXECUTE" call executes a named statement in a named package. If the prepped statement contains parameter markers or host variables, the substitution data is passed in the USING clause of the EXECUTE call. After execution, a number of status variables are available as for a dynamic execution (SQLCODE, SQLERRM, _NROWS). If the executed statement is a SELECT, the selected columns are returned in REXX stems. REXX programs can execute statements from different packages within the same LUW.
Like the dynamic interface, the static interface allows to fetch single rows. The static fetch interface is initiated with a "REXXSQL OPEN statement_name" call, eventually followed by a USING clause. Each table row is fetched by a "REXXSQL FETCH statement_name" call. A "REXXSQL CLOSE statement_name" call terminates the fetch sequence. Since each fetch sequence is identified by a statement name (which corresponds to an open cursor), multiple FETCH sequences can be open concurrently.
The "REXXSQL LOCATE packagename" call can be used to determine whether a package exists and to automatically generate it when it does not, as shown in the following example.
Stored Procedure support provided by REXX/SQL