SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION SAILPOINT.ES_V2_GET_EMPLOYEE_NUMBER ( 
	USERNAME CHAR(10) ) 
	RETURNS CLOB(2147483647)   
	LANGUAGE SQL 
	SPECIFIC SAILPOINT.V2_ESMEMPNO 
	NOT DETERMINISTIC 
	MODIFIES SQL DATA 
	CALLED ON NULL INPUT 
	NOT FENCED 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DECRESULT = (31, 31, 00) , 
	DLYPRP = *NO , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *USER , 
	SRTSEQ = *HEX   
	BEGIN 
DECLARE GLOBAL TEMPORARY TABLE SESSION . EMPNO_LIST ( 
ENVID CHAR ( 2 ) , LIB CHAR ( 10 ) , EXUSER CHAR ( 10 ) , EMPNO CHAR ( 8 ) ) 
WITH REPLACE ; 
FOR SELECT DISTINCT A . ENVID , TRIM ( 'CESLIB' || 
CASE A . ENVID 
WHEN 'MM' THEN '' 
ELSE RIGHT ( A . ENVID , 1 ) 
END ) AS CESLIB 
FROM TABLE ( 
SELECT ROW_NUMBER ( ) OVER ( 
) AS RN , ENVID 
FROM JSON_TABLE ( 
SAILPOINT . ES_V2_GET_ALL_MAPICS_ENVIRONMENTS ( ) , 
'$.data[*]' 
COLUMNS ( 
ENVID VARCHAR ( 2 ) CCSID 37 PATH '$.envId' 
) 
) 
) A 
DO 
BEGIN 
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION 
BEGIN 
END ; 
SET CISTOOLS . SQL_STATEMENT = 
'INSERT INTO SESSION.EMPNO_LIST ' || 'Select ''' || 
ENVID || ''', ''' || CESLIB || ''', ExUser, Ext08A ' 
|| 'FROM ' || CESLIB || '.ESUEXP WHERE ExUser = ''' 
|| TRIM ( USERNAME ) || '''' ; 
EXECUTE IMMEDIATE CISTOOLS . SQL_STATEMENT ; 
END ; 
END FOR ; 
RETURN ( 
SELECT JSON_OBJECT ( 
'data' : 
JSON_ARRAYAGG ( 
JSON_OBJECT ( 
'envId' VALUE ENVID , 'userEmpno' VALUE TRIM ( EMPNO ) 
) FORMAT JSON 
) , 
'errors' : JSON_ARRAY ( ) 
) 
FROM SESSION . EMPNO_LIST ) ; 
END  ; 

GRANT ALTER , EXECUTE   
ON SPECIFIC FUNCTION SAILPOINT.V2_ESMEMPNO 
TO AMAPICS WITH GRANT OPTION ; 

GRANT EXECUTE   
ON SPECIFIC FUNCTION SAILPOINT.V2_ESMEMPNO 
TO PUBLIC ; 

