Version 4.2.6 for Mainframes
 —  Database Management System Interfaces  —

Writing a Natural Stored Procedure

This section provides a general guideline of how to write a Natural Stored Procedure and what to consider when writing it.

Start of instruction set To write a Natural stored procedure

  1. Determine the format and attributes of the parameters that are passed between the caller and the stored procedure. Consider creating a Natural PDA (parameter data area). Stored procedures do not support data groups and redefinition within their parameters.

  2. Determine the PARAMETER STYLE of the stored procedure: GENERAL, GENERAL WITH NULL or DB2SQL.

  3. Decide which and how many result sets the stored procedure will return to the caller.

  4. Code your stored procedure as a Natural subprogram.

  5. For DB2 UDB: Issue a CREATE PROCEDURE statement that defines your stored procedure, for example:

    CREATE PROCEDURE <PROCEDURE>
      (INOUT  STCB       VARCHAR(274+13*N),
       INOUT  <PARM1>    <FORMAT>,         
       INOUT  <PARM2>    <FORMAT>,         
       INOUT  <PARM3>    <FORMAT>       
       .                   
      )                                  
      DYNAMIC RESULT SET <RESULT_SETS>     
      EXTERNAL NAME <LOADMOD>  
      LANGUAGE ASSEMBLE 
      PROGRAM TYPE <PGM_TYPE>
      PARAMETER STYLE GENERAL <WITH NULLS depending on LINKAGE>;

    The data specified in angle brackets (< >) correspond to the data listed in the table above, PARM1 - PARM3 and FORMAT depend on the call parameter list of the stored procedure. See also Example Stored Procedure NDBPURGN, Member CR6PURGN.

  6. Code your Natural program invoking the stored procedure via the CALLDBPROC statement.

    Code the parameters in the CALLDBPROC statement in the same sequence as they are specified in the stored procedure. Define the parameters in the calling program in a format that is compatible with the format defined in the stored procedure.

    If you use result sets, specify a RESULT SETS clause in the CALLDBPROC statement followed by a number of result set locator variables of FORMAT (I4). The number of result set locator variables should be the same as the number or result sets created by the stored procedure. If you specify fewer than are created, some result sets are lost. If you specify more than are created, the remaining result set locator variables are lost. The sequence of locator variables corresponds to the sequence in which the result sets are created by the stored procedure.

    Keep in mind that the fields into which the result set rows are read have to correspond to the fields used in the SELECT WITH RETURN statement that created the result set.

Top of page