IBM Books

Net.Data Administration and Programming Guide for OS/400

Stored Procedures

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:

Stored Procedure Syntax

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:

function_name
Is the name of the Net.Data function that initiates the call of the stored procedure

stored_procedure
Is the name of the stored procedure

datatype
Is one of the database data types supported by Net.Data as shown in Table 2. The data types specified in the parameter list must match the data types in the stored procedure. See your database documentation for more information about these data types.

tablename
Is the name of a Net.Data table in which the result set is to be stored (used only when the result set is to be stored in a Net.Data table). If specified, this parameter name must match the associated parameter name for resultsetname.

resultsetname
A name that associates a result set returned from the stored procedure with a report block. This parameter name must match the associated parameter name for tablename if specified.

Table 2. Stored Procedures Data Types
CHAR FLOAT TIME
DATE GRAPHIC TIMESTAMP
DECIMAL INTEGER VARCHAR
DOUBLE REAL VARGRAPHIC
DOUBLEPRECISION SMALLINT






Calling a Stored Procedure

To call a stored procedure:

  1. Define a function that initiates a call to the stored procedure.
    %function (dtw_sql) function_name() 
    

  2. Optionally, specify any IN, INOUT, or OUT parameters for the stored procedure, including the result set name of any result sets that are returned from the stored procedure.
    %function (dtw_sql) function_name (IN datatype
    arg1, INOUT datatype arg2, OUT tablename...) 
    

  3. Use the CALL statement to identify the stored procedure name.
     CALL stored_procedure
    

  4. If the stored procedure is going to generate one result set, optionally specify a REPORT block to define how Net.Data displays the result set.
    %report  {
    ...
    %}
    

    Example:

    %function (dtw_sql) mystoredproc (IN CHAR(30) arg1 OUT mytable)  {
         CALL myproc   
     %report {
      ...
      %row {  ...   %}
      ...   
     %} 
    %}
    

  5. If the stored procedure is going to generate more than one result set:

Passing Parameters

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  
...

Processing Result Sets

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 {  ...   %}     
  ...  
 %} 
%}


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]