DYNAMIC RESULT SETS 1
LANGUAGE SQL
BEGIN

        BEGIN


            DECLARE GLOBAL TEMPORARY TABLE SESSION.programWithAlias
			(
                 ProgramID     INTEGER,
				 ProgramName   VARCHAR(256),
				 ProgramTypeID INTEGER,
				 OccurID       INTEGER,
				 Ancestor  VARCHAR(256)
			) WITH REPLACE 
              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 CallingPath,
						            Programs_1.Ancestor AS AncestorName,
						            Programs_2.Ancestor AS CalledAncestorName,
						            Ancestor.ProgramID AS CallingAncestorID,
						            os.StartRow,
						            os.EndRow,
						            os.StartCol,
						            os.EndCol,
						            CAST(NULL AS VARCHAR(50)) AS ResourceName,
						            sr.OccurID
						     FROM Paths Paths_1
						          INNER JOIN Occurrences occ ON Paths_1.PathID = occ.PathID
						          INNER JOIN SESSION.programWithAlias Programs_1  ON occ.OccurID = Programs_1.OccurID
						          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
						          LEFT OUTER JOIN SESSION.programWithAlias Ancestor ON Programs_1.Ancestor = Ancestor.ProgramName
								  RIGHT OUTER JOIN ProgramTypes ProgramTypes_1 ON ProgramTypes_1.ProgramTypeID = Programs_1.ProgramTypeID
						     WHERE sr.ResourceType = 5
						           AND Programs_2.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 CallingPath,
									Programs_1.Ancestor AS AncestorName,
									Programs_2.Ancestor AS CalledAncestorName,
									Ancestor.ProgramID AS CallingAncestorID,
									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
								   INNER JOIN SESSION.programWithAlias Programs_1 ON occ.OccurID = Programs_1.OccurID
								   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
								   LEFT JOIN SESSION.programWithAlias Ancestor ON Programs_1.Ancestor = Ancestor.ProgramName
								   RIGHT OUTER JOIN ProgramTypes ProgramTypes_1 ON ProgramTypes_1.ProgramTypeID = Programs_1.ProgramTypeID
							   WHERE sr.ResourceType = 14
							         AND Programs_2.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	
