Below is how you can create a SQL stored procedure that calls the CL command ADDLIBLE to change the library list before calling an external RPG program that may require that Library in the Library List to run properlyWe also handle the error scenario where you might add a library that already exists (which would crash your SQL stored proc).  Let me know if this is helpful by commenting below or tweeting a reply to @php_dave

CREATE PROCEDURE MYLIB.SQLSTOREDPROCEDURE (
IN MYNUMBER DECIMAL(9, 0),
INOUT RESULT CHAR(1) )
LANGUAGE SQL
BEGIN
DECLARE CMD CHAR ( 300 ) DEFAULT ' ' ;
DECLARE CURRENTDATE CHAR ( 8 ) DEFAULT ' ' ;
Ignore the error message that occurs if you add the library list twice
Create a variable that has the SQLSTATE value and create a CONTINUE HANDLER for that
SQLSTATE
DECLARE LibraryAlreadyAdded CONDITION FOR SQLSTATE '38501';
DECLARE CONTINUE HANDLER FOR LibraryAlreadyAdded BEGIN END;
SET CMD = 'ADDLIBLE MYLIB2' ;
SET CURRENTDATE = YEAR(NOW()) || RIGHT('0' || MONTH(NOW()),2) || RIGHT('0' || DAY(NOW()),2) ;
CALL Command to add MYLIB2 to the Library list
CALL QSYS . QCMDEXC ( CMD , 0000000300.00000 ) ;
CALL EXTERNAL RPG PROGRAM WITH INOUT PARAM OUTPUTDATE
CALL MYLIB.MYRPGPGM (MYNUMBER,OUTPUTDATE);
IF (CURRENTDATE>OUTPUTDATE) THEN
SET RESULT = 'N';
ELSE
SET RESULT = 'Y';
END IF;
END ;
Call the SQL Stored Procedure with a value
CALL MYLIB.SQLSTOREDPROCEDURE(987654321,' ');