IBM Support

External CL Program Stored Procedures and Output Parameters

Troubleshooting


Problem

This document explains how to work with external CL programs as stored procedures that must return output parameters.

Resolving The Problem

When creating an externally stored procedure that references a CL program that must return output parameters, there are a few considerations.

Whenever you are working with CL stored procedures, PARAMETER STYLE GENERAL must be used.

If using an IN parameter, provide a value up to the length specified on the CREATE PROCEDURE and in the DCL for the CL. If this is a string, include single quotes around the value. If using an INOUT parameter, pass a value into the procedure rather than a question mark. The value will be used as input but will also act as an output parameter described next. If using OUT parameters, the call should have that position filled with a question mark (?). Inside the CL program, CHGVARs are used to manipulate the value for that parameter as declared on the PGM line. Whatever the value of the variable when the CL ends is what is returned as the output parameter.

Here is a sample CL that receives an IN parameter and then sets the output parameter to the same value.

       *************** Beginning of data ************************
0001.00              PGM        PARM(&INPARM &OUTPARM)            
0002.00              DCL        VAR(&INPARM) TYPE(*CHAR) LEN(10)  
0003.00              DCL        VAR(&OUTPARM) TYPE(*CHAR) LEN(10)
0004.00              CHGVAR     VAR(&OUTPARM) VALUE(&INPARM)      
0005.00              ENDPGM                                      
       ****************** End of data ***************************

The CREATE PROCEDURE statement could look like either of the following depending on if you use an INOUT or only OUT:

CREATE PROCEDURE YOURLIB.PARMTEST (
IN PARM1 CHAR(10) ,
INOUT PARM2 CHAR(10) )
LANGUAGE CL
EXTERNAL NAME 'YOURLIB/PARMTEST'
PARAMETER STYLE GENERAL

CREATE PROCEDURE YOURLIB.PARMTEST (
IN PARM1 CHAR(10) ,
OUT PARM2 CHAR(10) )
LANGUAGE CL
EXTERNAL NAME 'YOURLIB/PARMTEST'
PARAMETER STYLE GENERAL

To run the stored procedure, the CALL looks similar to the following (the first for INOUT and the second for OUT):

CALL YOURLIB.PARMTEST('ABC123', 'garbage')

CALL YOURLIB.PARMTEST('ABC123', ?)

If you run this in Run SQL Scripts, in the message area you will see:

> CALL YOURLIB.PARMTEST('ABC123', ?)

Output Parameter #2 = ABC123

Statement ran successfully (16 ms)

[{"Type":"MASTER","Line of Business":{"code":"LOB68","label":"Power HW"},"Business Unit":{"code":"BU070","label":"IBM Infrastructure"},"Product":{"code":"SWG60","label":"IBM i"},"ARM Category":[{"code":"a8m0z0000001iICAAY","label":"IBM i Db2-\u003EStored Procedures"}],"ARM Case Number":"","Platform":[{"code":"PF012","label":"IBM i"}],"Version":"All Versions"}]

Historical Number

361621157

Document Information

More support for:
IBM i

Component:
IBM i Db2->Stored Procedures

Software version:
All Versions

Operating system(s):
IBM i

Document number:
638669

Modified date:
25 November 2024

UID

nas8N1015783

Manage My Notification Subscriptions