SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION CISTOOLS.GET_XA_SUBSET ( 
	XA_TOKEN CHAR(10) ) 
	RETURNS CLOB(1048576)   
	LANGUAGE SQL 
	SPECIFIC CISTOOLS.GET_XA_SCUBSET 
	NOT DETERMINISTIC 
	MODIFIES SQL DATA 
	CALLED ON NULL INPUT 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DBGVIEW = *SOURCE , 
	DECRESULT = (31, 31, 00) , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *OWNER , 
	SRTSEQ = *HEX   
	BEGIN 
DECLARE PAD CHAR ( 15 ) DEFAULT '' ; 
DECLARE XA_COUNT , DONE SMALLINT DEFAULT 0 ; 
DECLARE RETURN_DATA CLOB ( 1048576 ) DEFAULT '' ; 
DECLARE XA_CURSOR CURSOR FOR XA_STATEMENT ; 
DECLARE CONTINUE HANDLER FOR NOT FOUND SET DONE = 1 ; 
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET DONE = 1 ; 
SET SQL_STATEMENT = '' ; 
SET DONE = 0 ; 
FOR XA_SQL AS SELECT 
TRIM ( SUBSTR ( SLCL , 1 , LOCATE ( 'FOR FETCH ONLY' , UPPER ( SLCL ) ) - 1 ) )AS XA_SQL 
FROM MSCSLT 
WHERE TSGP = XA_TOKEN 
DO 
IF DONE = 0 THEN 
SET XA_COUNT = XA_COUNT + 1 ; 
IF XA_COUNT = 1 THEN 
EXECUTE IMMEDIATE 
'Declare Global Temporary Table Session.' || XA_TOKEN || 
' As 
                     (Select *
                           From Table(' ||XA_SQL || '))
                     With Data
               With Replace' ; 
ELSE 
EXECUTE IMMEDIATE 'Insert Into Session.' || XA_TOKEN || 
' Select *
                  From Table(' || XA_SQL || ')' ; 
END IF ; 
END IF ; 
END FOR ; 
PREPARE XA_STATEMENT FROM 
'Values Generate_JSON(''Select * From Session.' || XA_TOKEN || 
''') Into ?' ; 
EXECUTE XA_STATEMENT USING RETURN_DATA ; 
RETURN RETURN_DATA ; 
END  ; 

COMMENT ON PARAMETER SPECIFIC FUNCTION CISTOOLS.GET_XA_SCUBSET 
( XA_TOKEN IS 'XA Token' ) ; 

LABEL ON SPECIFIC FUNCTION CISTOOLS.GET_XA_SCUBSET 
	IS 'Get Xa Subset' ; 

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

GRANT EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.GET_XA_SCUBSET 
TO PUBLIC ; 

