DYNAMIC RESULT SETS 1
LANGUAGE SQL
ss: BEGIN

        -- clean up temp table if exists 
		BEGIN
			DECLARE CONTINUE HANDLER FOR SQLSTATE '42704'
			BEGIN END;
			
			COMMIT;
			DROP TABLE SESSION.programWithAlias;
			
		END;


        BEGIN


            DECLARE GLOBAL TEMPORARY TABLE SESSION.programWithAlias
			( 
                 ProgramID     INTEGER,
				 ProgramName   VARCHAR(256),
				 ProgramTypeID INTEGER,
				 OccurID       INTEGER,
				 Ancestor  VARCHAR(256)
			) ON COMMIT PRESERVE ROWS NOT LOGGED ;

            INSERT INTO SESSION.programWithAlias (ProgramID, ProgramName, ProgramTypeID, OccurID, Ancestor)
            SELECT  prgx.ProgramID, 
			        prax.AliasName AS ProgramName,
                    prgx.ProgramTypeID,
					prgx.OccurID,
					prgx.Ancestor
            FROM ProgramAliases prax
                INNER JOIN Programs prgx ON prax.ProgramId = prgx.ProgramID
            WHERE prax.AliasType = 0
			;


            BEGIN
					    DECLARE crs CURSOR WITH RETURN FOR
					    SELECT *
					    FROM (
						 SELECT    Programs_1.ProgramID,
							       Programs_1.ProgramName,
							       ProgramTypes_1.ProgramTypeID,
							       st.Description,
							       Programs_2.ProgramID AS CalledProgId,
							       Programs_2.ProgramName AS CalledProgramName,
							       Programs_2.ProgramTypeID AS CalledProgramTypeID,
							       os.StatementType,
							       pth.PathStr,
							       Paths_1.PathStr AS Path2,
							       Programs_1.Ancestor AS AncestorName,
							       Programs_2.Ancestor AS CalledAncestorName,
							       Ancestor.ProgramID AS CalledAncestorID,
							       os.StartRow,
							       os.EndRow,
							       os.StartCol,
							       os.EndCol,
							       CAST(NULL AS VARCHAR(50)) AS ResourcesName,
							       sr.OccurID
							FROM Paths Paths_1
							     INNER JOIN Occurrences occ ON Paths_1.PathID = occ.PathID
							     RIGHT OUTER JOIN ProgramTypes ProgramTypes_1
							     INNER JOIN SESSION.programWithAlias Programs_1 ON ProgramTypes_1.ProgramTypeID = Programs_1.ProgramTypeID
							     INNER JOIN OccurrencesStmt os ON Programs_1.ProgramID = os.ProgID
								 INNER JOIN StatementReference sr ON sr.OccurID = os.OccurID
							     INNER JOIN Statements st ON os.StatementType = st.StatementType 
							     INNER JOIN SESSION.programWithAlias Programs_2 ON sr.ResourceID = Programs_2.ProgramID
							     INNER JOIN Paths pth ON os.PathID = pth.PathID ON occ.OccurID = Programs_2.OccurID
							     LEFT OUTER JOIN SESSION.programWithAlias Ancestor ON UPPER(Programs_2.Ancestor) = UPPER(Ancestor.ProgramName)
     						 WHERE sr.ResourceType = 5
							      AND Programs_1.ProgramID IN (SELECT * FROM SESSION.numeric_param_temp)
							      AND (Ancestor.ProgramTypeID IS NULL OR Ancestor.ProgramTypeID NOT IN (15, 16, 19))
							     
							     
							UNION
							   
							SELECT Programs_1.ProgramID,
							       Programs_1.ProgramName,
							       ProgramTypes_1.ProgramTypeId,
							       st.Description,
							       Programs_2.ProgramID AS CalledProgId,
							       Programs_2.ProgramName AS CalledProgramName,
							       Programs_2.ProgramTypeID AS CalledProgramTypeID,
							       os.StatementType,
							       pth.PathStr,
							       Paths_1.PathStr AS Path2,
							       Programs_1.Ancestor AS AncestorName,
							       Programs_2.Ancestor AS CalledAncestorName,
							       Ancestor.ProgramID AS CalledAncestorID,
							       os.StartRow,
							       os.EndRow,
							       os.StartCol,
							       os.EndCol,
							       res.Name,
							       sr.OccurID
							FROM Paths Paths_1
							     INNER JOIN Occurrences occ ON Paths_1.PathID = occ.PathID
							     RIGHT OUTER JOIN ProgramTypes ProgramTypes_1
							     INNER JOIN SESSION.programWithAlias Programs_1 ON ProgramTypes_1.ProgramTypeID = Programs_1.ProgramTypeID
							     INNER JOIN OccurrencesStmt os ON Programs_1.ProgramID = os.ProgID
								 INNER JOIN StatementReference sr ON sr.OccurID = os.OccurID
							     INNER JOIN Statements st ON os.StatementType = st.StatementType 
							     INNER JOIN Resources res ON sr.ResourceID = res.ResourceID
							     INNER JOIN SESSION.programWithAlias Programs_2 ON res.ProgName = Programs_2.ProgramName
							     INNER JOIN Paths pth ON os.PathID = pth.PathID ON occ.OccurID = Programs_2.OccurID
							     LEFT OUTER JOIN SESSION.programWithAlias Ancestor ON UPPER(Programs_2.Ancestor) = UPPER(Ancestor.ProgramName)
							WHERE sr.ResourceType = 14
							          AND Programs_1.ProgramID IN (SELECT * FROM SESSION.numeric_param_temp)
							          AND (Ancestor.ProgramTypeID IS NULL OR Ancestor.ProgramTypeID NOT IN (15, 16, 19))
							 )src
							ORDER BY src.ProgramID,
							         src.OccurID,
							         src.CalledProgramTypeID DESC;
						 

					           
				       OPEN crs;
				
                END;
                
          END ; 
END ss	





