A stored procedure is a compiled program, stored at the DB2 local or remote server, that can execute SQL statements. In Net.Data, stored procedures are called from Net.Data functions using the CALL SQL statement. Stored procedure parameters are passed in from the Net.Data function parameters list. You can use stored procedures to improve performance and integrity by keeping compiled SQL statements with the database server.
This section describes following topics:
The syntax of the stored procedure uses the FUNCTION statement, the CALL statement, and optionally a REPORT block.
%function (dtw_sql) function_name ([IN datatype arg1, INOUT datatype arg2,
OUT tablename, ...])
CALL stored_procedure
[%REPORT(resultsetname...)]
Where:
Table 2. Stored Procedures Data Types
| CHAR | FLOAT | TIME |
| DATE | GRAPHIC | TIMESTAMP |
| DECIMAL | INTEGER | VARCHAR |
| DOUBLE | REAL | VARGRAPHIC |
| DOUBLEPRECISION | SMALLINT |
|
|
|
|
|
|
|
|
|
To call a stored procedure:
%function (dtw_sql) function_name()
%function (dtw_sql) function_name (IN datatype arg1, INOUT datatype arg2, OUT tablename...)
CALL stored_procedure
%report {
...
%}
Example:
%function (dtw_sql) mystoredproc (IN CHAR(30) arg1 OUT mytable) {
CALL myproc
%report {
...
%row { ... %}
...
%}
%}
%function (dtw_sql) function_name (OUT tablename, ...)
%REPORT(resultsetname1) {
...
%}
Example:
%function (dtw_sql) mystoredproc (IN CHAR(30) arg1, OUT table1, table2) {
CALL myproc
%report (table1) {
...
%row { ... %}
...
%}
%report (table1) {
...
%row { ... %}
...
%}
%}
You can pass parameters to a stored procedure and you can have the stored procedure update the parameter values so that the new value is passed back to the Net.Data macro. You pass a parameter in to the stored procedure by specifying the parameter name with IN keyword. If the stored procedure is going to update the parameter, you must pass the parameter for the returned value with the INOUT or OUT keywords. The data type that is specified for a parameter must match what the stored procedure is expecting.
Example 1: Passing a parameter value to the stored procedure
%function (dtw_sql) mystoredproc (IN CHAR(30) valuein) {
CALL myproc
...
Example 2: Returning a value from a stored procedure
%function (dtw_sql) mystoredproc (OUT VARCHAR(9) retvalue) {
CALL myproc
...
You can return one or more result sets from a stored procedure. The result sets can be stored in Net.Data tables for further processing within your macro or displayed using a REPORT block. You must associate a name with each result set generated by the stored procedure. This is done by specifying parameters on the FUNCTION statement. The name you specify for a result set can then be associated with a REPORT block or a Net.Data table, enabling you to determine how each result set is processed by Net.Data. You can:
Result sets are always stored in local tables so that another function can use the data later within the macro file. For example, you can pass a Net.Data table to another function so that it can use the data for calculations and display the results based on those calculations.
See Guidelines and Restrictions for Multiple REPORT Blocks for guidelines and restriction when using multiple report blocks.
To return a single result set and display it using a default report:
Use the following syntax:
%function (dtw_sql) function_name (OUT tablename) {
CALL stored_procedure
%}
For example:
%function (dtw_sql) mystoredproc(OUT mytable1) {
CALL myproc
%}
To return a single result set and specify a REPORT block for display processing:
Use the following syntax:
%function (dtw_sql) function_name (OUT tablename) {
CALL stored_procedure
%REPORT (resultsetname) {
...
%}
%}
For example:
%function (dtw_sql) mystoredproc (OUT mytable1) {
CALL myproc
%REPORT (mytable1) {
...
%row { ... %}
...
%}
%}
Alternatively, the following syntax can be used:
%function (dtw_sql) function_name () {
CALL stored_procedure
%REPORT () {
...
%}
%}
For example:
%function (dtw_sql) mystoredproc () {
CALL myproc
%REPORT {
...
%row { ... %}
...
%}
%}
To return multiple result sets and display them using default report formatting:
Use the following syntax:
%function (dtw_sql) function_name (OUT tablename1, tablename2) {
CALL stored_procedure
%}
Where no report block is specified.
For example:
%define DTW_DEFAULT_REPORT = "MULTIPLE"
%function (dtw_sql) mystoredproc (OUT mytable1, mytable2) {
CALL myproc
%}
To return multiple result sets and specify REPORT blocks for display processing:
Each result set is associated with its own REPORT block. Use the following syntax:
%function (dtw_sql) function_name (OUT tablename1, tablename2) {
CALL stored_procedure
%REPORT (resultsetname1)
...
%row { ... %}
...
%}
%REPORT (resultsetname2)
...
%row { ... %}
...
%}
%}
For example:
%function (dtw_sql) mystoredproc (OUT mytable1, mytable2) {
CALL myproc
%REPORT(mytable1) {
...
%row { ... %}
...
%}
%REPORT(mytable2) {
...
%row { ... %}
...
%}
%}