External stored procedure calling RPGLE program with no parameter |
An external stored procedure is a procedure that is written in a host language and can consist of SQL statements. The source code for the external procedure is separate from the create script(definition).
RPGLE program EXTPGM1 for External stored procedure
D n1 S 10i 0 inz(1) D n2 S 10a inz('A') /Free EXEC SQL INSERT INTO EASYCLASS1.EXTPF1 (FLD1,FLD2) VALUES(:n1,:n2); *INLR = *ON; /End-Free
Here, we have taken two variables n1 and n2 respectively of type integer and character and initialized with value 1 and A respectively. Then inserting the record into the file named EXTPF1 in column FLD1 and FLD2 using the host variable n1 and n2 of type integer and character. We use colon(:) to indicate a host variable in embedded SQL statements.
External Stored Procedure script in SQLRPLE program
Below program script for create procedure will create the external stored procedure using SQL Execute Immediate.
D sqlproc S 500a inz(*blanks) /Free sqlproc = 'CREATE PROCEDURE EASYCLASS1.EXTERNALSTOREDPROC1() ' + 'LANGUAGE RPGLE ' + 'SPECIFIC EASYCLASS1.EXTPROCED1 ' + 'EXTERNAL NAME EASYCLASS1.EXTPGM1 ' + 'MODIFIES SQL DATA ' + 'PARAMETER STYLE GENERAL'; EXEC SQL EXECUTE IMMEDIATE :sqlproc; *INLR = *ON; /End-Free
Explanation of the above code
sqlproc = 'CREATE PROCEDURE EASYCLASS1.EXTERNALSTOREDPROC1() ' + 'LANGUAGE RPGLE ' +
'SPECIFIC EASYCLASS1.EXTPROCED1 ' +
'EXTERNAL NAME EASYCLASS1.EXTPGM1 ' +
'MODIFIES SQL DATA ' +
'PARAMETER STYLE GENERAL';
EXEC SQL EXECUTE IMMEDIATE :sqlproc; *INLR = *ON; /End-Free
How to call External stored procedure in RPGLE
/Free EXEC SQL CALL EASYCLASS1.EXTERNALSTOREDPROC1(); *INLR = *ON; /End-Free
Here, EXEC SQL CALL EASYCLASS1.EXTERNALSTOREDPROC1() command is used to execute a stored procedure which is present in library EASYCLASS1 and we always need a name which is used just after create procedure i.e. long name of the procedure, do not use specific name to call a procedure, we can drop a stored procedure using specific name. INLR=*ON is used to indicate that last record has been reached or you can say that is used to terminate the program.