External stored procedure calling SRVPGM procedure with Input and Output parameter |
In my previous article where we are calling the RPGLE Program directly but in this article we are not calling the program we are going to call the the Service program (SRVPGM) procedure.
Let's see the example, we have a Service program (SRVPGM) named EXTSRVPGM4
H NOMAIN Dsubproc1 pr D 10i 0 D 10a D 10i 0 Psubproc1 B EXPORT D subproc1 pi D n1 10i 0 D n2 10a D l_sqlcode 10i 0 /Free EXEC SQL INSERT INTO EASYCLASS1.EXTPF1 (FLD1,FLD2) VALUES(:n1,:n2); l_sqlcode = sqlcode; return; /End-Free Psubproc1 E
Explanation of the above code
H NOMAIN
H NOMAIN Dsubproc1 pr D 10i 0 D 10a D 10i 0
Psubproc1 B EXPORT
D subproc1 pi D n1 10i 0 D n2 10a D l_sqlcode 10i 0
/Free EXEC SQL INSERT INTO EASYCLASS1.EXTPF1 (FLD1,FLD2) VALUES(:n1,:n2);
l_sqlcode = sqlcode; return; /End-Free
Psubproc1 E
Example of an External Stored Procedure Calling a SRVPGM procedure SUBPROC1 with Input and Output parameter
D sqlproc S 500a inz(*blanks) /Free sqlproc = 'CREATE PROCEDURE EASYCLASS1.EXTERNALSTOREDPROC4( ' + 'IN PARM1 INTEGER, IN PARM2 CHAR(10), ' + 'OUT PARM3 INTEGER) ' + 'LANGUAGE RPGLE ' + 'SPECIFIC EASYCLASS1.EXTPROCED4 ' + 'EXTERNAL NAME EASYCLASS1.EXTSRVPGM4(SUBPROC1) ' + 'MODIFIES SQL DATA ' + 'PARAMETER STYLE GENERAL'; EXEC SQL EXECUTE IMMEDIATE :sqlproc; *INLR = *ON; /End-Free
Explanation of the above code
D sqlproc S 500a inz(*blanks)
/Free sqlproc = 'CREATE PROCEDURE EASYCLASS1.EXTERNALSTOREDPROC4( ' + 'IN PARM1 INTEGER, IN PARM2 CHAR(10), ' + 'OUT PARM3 INTEGER) ' + 'LANGUAGE RPGLE ' +
'SPECIFIC EASYCLASS1.EXTPROCED4 ' +
'EXTERNAL NAME EASYCLASS1.EXTSRVPGM4(SUBPROC1) ' +
'MODIFIES SQL DATA ' +
'PARAMETER STYLE GENERAL';
EXEC SQL EXECUTE IMMEDIATE :sqlproc; *INLR = *ON; /End-Free
Calling the external stored procedure in the RPGLE program
D parm1 s 10i 0 inz(4) D parm2 s 10a inz('TEST4') D parm3 s 10i 0 inz(-1) /Free EXEC SQL CALL EASYCLASS1.EXTERNALSTOREDPROC4(:parm1,:parm2,:parm3); *INLR = *ON; /End-Free
Explanation of the above code
Here, we need to call an external stored procedure so we can call using SQL CALL then we have to write EXEC SQL and write CALL EASYCLASS1.EXTERNALSTOREDPROC4(:parm1,:parm2,:parm3) where we have two input params parm1 and parm2 where one of integer 10i 0 inz(3) and one of character 10a inz('TEST4') and both are initialized with (4) and (TEST4) and third parm is output parameter parm3 whose type is integer 10i 0 inz(-1) initialized with (-1) so that we will be getting some feedback from the program which is EXTERNALSTOREDPROC4. whether SQL Insert executed with success or with errors.