SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION CISTOOLS.SET_LIBRARY_LIST ( 
	INENV CHAR(2) ) 
	RETURNS VARCHAR(1000)   
	LANGUAGE SQL 
	SPECIFIC CISTOOLS.SETLIBL 
	NOT DETERMINISTIC 
	MODIFIES SQL DATA 
	CALLED ON NULL INPUT 
	NOT FENCED 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DBGVIEW = *SOURCE , 
	DECRESULT = (31, 31, 00) , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *OWNER , 
	SRTSEQ = *HEX   
	BEGIN 
DECLARE LIB_COUNT INT DEFAULT 0 ; 
DECLARE LIB_LIST VARCHAR ( 1000 ) DEFAULT '' ; 
SET CISTOOLS . MAPICS_ENV = '' ; 
SET LIB_LIST = ( 
SELECT LISTAGG ( FRAGMENT ) 
FROM TABLE ( 
CISTOOLS . SPLIT_STRING ( 
( 
SELECT 
CAST ( 
RPAD ( LIB01 , 11 , ' ' ) || 
RPAD ( LIB02 , 11 , ' ' ) || 
RPAD ( LIB03 , 11 , ' ' ) || 
RPAD ( LIB04 , 11 , ' ' ) || 
RPAD ( LIB05 , 11 , ' ' ) || 
RPAD ( LIB06 , 11 , ' ' ) || 
RPAD ( LIB07 , 11 , ' ' ) || 
RPAD ( LIB08 , 11 , ' ' ) || 
RPAD ( LIB09 , 11 , ' ' ) || 
RPAD ( LIB10 , 11 , ' ' ) || 
RPAD ( LIB11 , 11 , ' ' ) || 
RPAD ( LIB12 , 11 , ' ' ) || 
RPAD ( LIB13 , 11 , ' ' ) || 
RPAD ( LIB14 , 11 , ' ' ) || 
RPAD ( LIB15 , 11 , ' ' ) || 
RPAD ( LIB16 , 11 , ' ' ) || 
RPAD ( LIB17 , 11 , ' ' ) || 
RPAD ( LIB18 , 11 , ' ' ) || 
RPAD ( LIB19 , 11 , ' ' ) || 
RPAD ( LIB20 , 11 , ' ' ) || 
RPAD ( LIB21 , 11 , ' ' ) || 
RPAD ( LIB22 , 11 , ' ' ) || 
RPAD ( LIB23 , 11 , ' ' ) || 
RPAD ( LIB24 , 11 , ' ' ) || 
RPAD ( LIB25 , 11 , ' ' ) AS VARCHAR ( 1000 ) 
CCSID 37 ) AS LIBLIST 
FROM QGPL . MMLIST 
WHERE LIDES || FIDES = INENV ) , 11 ) 
) 
WHERE EXISTS ( 
SELECT 1 
FROM TABLE ( 
QSYS2 . SCHEMAS ( ) 
) 
WHERE TRIM ( FRAGMENT ) = ODOBNM ) ) ; 
SET LIB_COUNT = ( SELECT COUNT ( * ) 
FROM TABLE ( 
CISTOOLS . SPLIT_STRING ( LIB_LIST , 11 ) 
) 
WHERE FRAGMENT <> '' ) ; 
CALL CISTOOLS . CHANGE_LIBRARY_LIST ( CUR_LIB => RPAD ( 
'CISTOOLS' , 11 , ' ' ) , NEW_LIBRARY_LIST => LIB_LIST , 
NUM_LIB => LIB_COUNT ) ; 
SET CISTOOLS . MAPICS_ENV = INENV ; 
RETURN ( SELECT JSON_OBJECT ( 
INENV VALUE JSON_ARRAYAGG ( 
SCHEMA_NAME 
) 
) 
FROM LIBLIST ) ; 
END  ; 

GRANT ALTER , EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.SETLIBL 
TO AMAPICS WITH GRANT OPTION ; 

GRANT EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.SETLIBL 
TO PUBLIC ; 

