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
			 WHERE t.ProgramID IN (SELECT * FROM SESSION.numeric_param_temp) 
			;

	            BEGIN
                    
					     DECLARE crs CURSOR WITH RETURN FOR
					     SELECT *
					     FROM
					         (
					                 SELECT prg.ProgramID,
								            prg.ProgramName,
								            pth.PathStr,
								            occ1.StartRow,
								            CASE
								                WHEN pth.PathID = occ1.PathID
								                THEN 0
								                ELSE -1
								            END AS IsCopy
								     FROM ProgramElements pe
								          INNER JOIN Occurrences occ1 ON pe.OccurId = occ1.OccurID
								          INNER JOIN SESSION.programWithAlias prg ON pe.ProgId = prg.ProgramID
								          INNER JOIN Paths pth ON occ1.PathID = pth.PathID
								          INNER JOIN Occurrences occ2 ON prg.OccurID = occ2.OccurID
								     WHERE pe.ElementType IN (1, 2, 3, 4)
								          --AND prg.ProgramID IN (SELECT * FROM SESSION.numeric_param_temp)
								          AND occ1.StartCol <> 8
								    
								     UNION
								     SELECT DISTINCT
								            prg.ProgramID,
								            prg.ProgramName,
								            pth.PathStr,
								            occ2.StartRow,
								            CASE
								                WHEN pth.PathID = occ1.PathID
								                THEN 0
								                ELSE -1
								            END AS IsCopy
								     FROM Paths pth
								          INNER JOIN Occurrences occ2 ON pth.PathID = occ2.PathID
										  INNER JOIN Sections ON occ2.OccurID = Sections.OccurID
										  INNER JOIN SESSION.programWithAlias prg ON prg.ProgramID = Sections.ProgramID
								          INNER JOIN Occurrences occ1 ON prg.OccurID = occ1.OccurID 
								     WHERE occ2.StartCol <> 8
									       --AND prg.ProgramID IN (SELECT * FROM SESSION.numeric_param_temp)
								       
								     
									UNION
								     SELECT DISTINCT prg.ProgramID,
								                     prg.ProgramName,
								                     pth.PathStr,
								                     occ2.StartRow,
								                    CASE
								                        WHEN pth.PathID = occ1.PathID
								                        THEN 0
								                        ELSE -1
								                    END AS IsCopy
								     FROM Paths pth
								          INNER JOIN Occurrences occ2 ON pth.PathID = occ2.PathID
								          INNER JOIN Paragraphs par ON occ2.OccurID = par.OccurID
								          INNER JOIN SESSION.programWithAlias prg ON par.ProgramID = prg.ProgramID
										  INNER JOIN Occurrences occ1 ON occ1.OccurID = prg.OccurID 
								     WHERE occ2.StartCol <> 8 
									       --AND prg.ProgramID IN (SELECT * FROM SESSION.numeric_param_temp)
								           AND UPPER(par.ParaName) NOT LIKE '%_FIRST_SENTENCES'
					         
					         
					         ) s
					      ORDER BY s.ProgramName, s.PathStr, s.StartRow;
							 			

				        OPEN crs;
				
                END;
				
	END;			
 		           
END ss	



