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,
				 AncestorID    INTEGER,
				 Ancestor      VARCHAR(256)
			) ON COMMIT PRESERVE ROWS NOT LOGGED ;

             INSERT INTO SESSION.programWithAlias (ProgramID, ProgramName, ProgramTypeID, OccurID, AncestorID, Ancestor)
             SELECT t.ProgramID, t.ProgramName, t.ProgramTypeID, t.OccurID, t.AncestorID, t.AncestorName
	         FROM TABLE( 
			            fGetProgram_Ancestor_Aliases( CAST(NULL AS INTEGER) ) 
			         )t
			;

	            BEGIN

					    DECLARE crs CURSOR WITH RETURN FOR
						SELECT  pgm.ProgramName,
					            res.Name AS Include_File_Name,
					            COUNT(res.Name) AS Expr1,
					            pgm.Ancestor,
								pgm.ProgramID,
								pgm.AncestorID
					     FROM StatementReference sr
					           INNER JOIN OccurrencesStmt os ON sr.OccurID = os.OccurID
							   INNER JOIN SESSION.programWithAlias pgm ON os.ProgID = pgm.ProgramID
					           INNER JOIN Resources res ON sr.ResourceID = res.ResourceID
					     WHERE sr.ResourceType IN (13, 60, 67, 99 ,57)
					     GROUP BY pgm.Ancestor,
					              pgm.ProgramName,
					              res.Name,
								  pgm.ProgramID,
								  pgm.AncestorID
					     ORDER BY pgm.Ancestor,
					              pgm.ProgramName,
					              res.Name,
								  pgm.ProgramID; 


				         OPEN crs;
				
				END;
				
		END;		

END ss	



