DYNAMIC RESULT SETS 1
LANGUAGE SQL
BEGIN

          BEGIN


            DECLARE GLOBAL TEMPORARY TABLE SESSION.programWithAlias
			(
                 ProgramID     INTEGER,
				 ProgramName   VARCHAR(256),
				 ProgramTypeID INTEGER,
				 OccurID       INTEGER,
				 AncestorID    INTEGER,
				 Ancestor      VARCHAR(256)
			) WITH REPLACE 
              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
			  WHERE t.ProgramID IN (SELECT * FROM SESSION.numeric_param_temp)		
			;

	            BEGIN
					    DECLARE crs CURSOR WITH RETURN FOR
					    SELECT   prg.ProgramName,
					             issr.RecordName,
					             st.Description AS Statement_Type,
					             COUNT(os.OccurID) AS Expr1,
					             prg.Ancestor,
								 prg.ProgramID,
                                 prg.AncestorID
					     FROM StatementReference sr
					            INNER JOIN OccurrencesStmt os ON sr.OccurID = os.OccurID
								INNER JOIN SESSION.programWithAlias prg ON os.ProgID = prg.ProgramID
					            INNER JOIN Statements st ON sr.StatementType = st.StatementType
					            INNER JOIN IdmsSubschemaRecords issr ON sr.ResourceID = issr.ID
					     WHERE sr.ResourceType = 77
					     GROUP BY prg.Ancestor,
					              prg.ProgramName,
					              issr.RecordName,
					              st.Description,
								  prg.ProgramID,
                                  prg.AncestorID
					     ORDER BY prg.Ancestor,
					              prg.ProgramName,
								  prg.ProgramID,
					              issr.RecordName;

				        OPEN crs;
				
                END;
				
		END;

		
END	
