When invoked, the DB2DARI stored procedure performs the following:
To use the SQLDA structure to pass values to the stored procedure, perform the following steps before calling the stored procedure:
If your application will be working with character strings defined as FOR BIT DATA, you need to initialize the SQLDAID field to indicate that the SQLDA includes FOR BIT DATA definitions and the SQLNAME field of each SQLVAR that defines a FOR BIT DATA element.
If your application will be working with large objects, that is, data with types of CLOB, BLOB, or DBCLOB, you will also need to initialize the secondary SQLVAR elements. For information on the SQLDA structure, refer to the SQL Reference.
Declare SQLVARs using the same approach discussed in Allocating Host Variables. In addition, the client application should set the indicator of output-only SQLVARs to -1 as discussed in Data Structure Manipulation. This will improve the performance of the parameter passing mechanism by avoiding having to pass the contents of the SQLDATA pointer, as only the indicator is sent. You should set the SQLTYPE field to a nullable data type for these parameters. If the SQLTYPE indicates a non-nullable data type, the indicator variable is not checked by the database manager.
The stored procedure is invoked by the SQL CALL statement and executes using data passed to it by the client application. Information is returned to the client application using the stored procedure's SQLDA structure.
The parameters of the SQL CALL statement are treated as both input and output parameters and are converted into the following format for the stored procedure:
SQL_API_RC SQL_API_FN proc_name( void *reserved1, void *reserved2, struct sqlda *inoutsqlda, struct sqlca *sqlca )
The SQL_API_FN is a macro that specifies the calling convention for a function that may vary across each supported operating system. This macro is required when you write stored procedures or UDFs.
Following is an example of how a CALL statement maps to a server's parameter list:
CALL OUTSRV (:empno:empind,:salary:salind)
The parameters to this call are converted into an SQLDA structure with two SQLVARs. The first SQLVAR points to the empno host variable and the empind indicator variable. The second SQLVAR points to the salary host variable and the salind indicator variable.
Note: | The SQLDA structure is not passed to the stored procedure if the number of elements, SQLD, is set to 0. In this case, if the SQLDA is not passed, the stored procedure receives a NULL pointer. |
The database manager automatically allocates a duplicate SQLDA structure at the database server. To reduce network traffic, it is important to indicate which host variables are input-only, and which ones are output-only. The client procedure should set the indicator of output-only SQLVARs to -1. The server procedure should set the indicator for input-only SQLVARs to -128. This allows the database manager to choose which SQLVARs are passed.
Note that an indicator variable is not reset if the client or the server sets it to a negative value (indicating that the SQLVAR should not be passed). If the host variable to which the SQLVAR refers is given a value in the stored procedure or the client code, its indicator variable should be set to zero or a positive value so that the value is passed. For example, consider a stored procedure which takes one output-only parameter, called as follows:
empind = -1; EXEC SQL CALL storproc(:empno:empind);
When the stored procedure sets the value for the first SQLVAR, it should also set the value of the indicator to a non-negative value so that the result is passed back to empno.
Table 53 summarizes the use of the various structure fields by the stored procedures application. In the table, sqlda is an SQLDA structure passed to the stored procedure and n is a numeric value indicating a specific SQLVAR element of the SQLDA. The numbers on the right refer to the notes following the table.
Table 53. Stored Procedures Parameter Variables
Input/Output SQLDA | sqlda.SQLDAID |
|
|
| 4 |
|
|
|
|
| ||
| sqlda.SQLDABC |
|
|
| 4 |
|
|
|
|
| ||
| sqlda.SQLN |
| 2 |
| 4 |
|
|
|
|
| ||
| sqlda.SQLD |
| 2 | 3 |
| 5 |
|
|
|
| ||
Input/Output SQLVAR | sqlda.n.SQLTYPE |
| 2 | 3 |
| 5 |
|
|
|
| ||
| sqlda.n.SQLLEN |
| 2 | 3 |
| 5 |
|
|
|
| ||
| sqlda.n.SQLDATA | 1 | 2 | 3 |
|
| 6 |
| 8 |
| ||
| sqlda.n.SQLIND | 1 | 2 | 3 |
|
| 6 |
| 8 | 9 | ||
| sqlda.n.SQLNAME.length |
|
|
|
|
| 6 | 7 |
|
| ||
| sqlda.n.SQLNAME.data |
|
|
|
|
| 6 | 7 |
|
| ||
| sqlda.n.SQLDATATYPE_NAME |
| 2 | 3 |
| 5 |
|
|
|
| ||
| sqlda.n.SQLLONGLEN |
| 2 | 3 |
| 5 |
|
|
|
| ||
| sqlda.n.SQLDATALEN | 1 | 2 | 3 |
|
| 6 | 7 |
|
| ||
SQLCA (all elements) |
|
|
|
|
|
| 6 | 7 |
|
| ||
|
The stored procedure runs using any information passed in the input variables of the SQLDA structure. Information is returned to the client in the output variables of the SQLDA. Do not change the value of the SQLD, SQLTYPE, and SQLLEN fields of the SQLDA, as these fields are compared to the original values set by the client application before data is returned. If they are different, one of the following SQLCODEs is returned:
In addition, do not change the pointer for the SQLDATA and the SQLIND fields, although you can change the value that is pointed to by these fields.
Note: | It is possible to use the same variable for both input and output. |
Before the stored procedure returns, SQLCA information should be explicitly copied to the SQLCA parameter of the stored procedure.
The return value of the stored procedure is never returned to the client application. It is used by the database manager to determine if the server procedure should be released from memory upon exit.
The stored procedure returns one of the following values:
If the stored procedure is invoked only once, it should return SQLZ_DISCONNECT_PROC.
If the client application issues multiple calls to invoke the same stored procedure, SQLZ_HOLD_PROC should be the return value of the stored procedure. The stored procedure will not be unloaded.
If SQLZ_HOLD_PROC is used, the last invocation of the stored procedure should return the value SQLZ_DISCONNECT_PROC to free the stored procedure library from main memory. Otherwise, the library remains in main memory until the database manager is stopped. As an alert to the stored procedure, the client application could pass a flag in one of the parameters indicating the final call.