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 -1 AS ResourceID,
						           'NA' AS Name,
						           st.Description,
						           os.StatementType,
						           prg.ProgramName,
						           os.StartRow,
						           os.StartCol,
						           os.EndRow,
						           os.EndCol,
						           pth.PathStr,
						           os.OccurID,
						           prg.ProgramID,
						           par.ParaID,
						           par.ParaName
						     FROM SESSION.programWithAlias prg
						          INNER JOIN OccurrencesStmt os ON prg.ProgramID = os.ProgID
						          INNER JOIN Statements st ON os.StatementType = st.StatementType
						          INNER JOIN Paths pth ON os.PathID = pth.PathID
						          INNER JOIN Paragraphs par ON os.ParaID = par.ParaID
						     WHERE    prg.ProgramTypeID = 1
						          AND UPPER(prg.ProgramName) IN (SELECT UPPER(Param) FROM SESSION.string_param_temp)
						          AND os.StatementType IN (1838, 1839)
						          AND os.OccurID NOT IN
												    (
													   SELECT sr.OccurID
													   FROM StatementReference sr
													   WHERE sr.ResourceType = 101
												    )
						
						      UNION
						
						     SELECT sr.ResourceID,
						            res.Name,
						            st.Description,
						            os.StatementType,
						            prg.ProgramName,
						            sr.StartRow,
						            sr.StartCol,
						            sr.StartRow AS EndRow,
						            sr.StartCol AS EndCol,
						            pth.PathStr,
						            os.OccurID,
						            prg.ProgramID,
						            par.ParaID,
						            par.ParaName
						     FROM SESSION.programWithAlias prg
						          INNER JOIN OccurrencesStmt os ON prg.ProgramID = os.ProgID
						          INNER JOIN StatementReference sr ON os.OccurID = sr.OccurID
						          INNER JOIN Statements st ON sr.StatementType = st.StatementType
						          INNER JOIN Resources res ON sr.ResourceID = res.ResourceID
						          INNER JOIN Paths pth ON os.PathID = pth.PathID
						          INNER JOIN Paragraphs par ON os.ParaID = par.ParaID
						     WHERE prg.ProgramTypeID = 1 
						          AND UPPER(prg.ProgramName) IN (SELECT UPPER(Param) FROM SESSION.string_param_temp)
						          AND os.StatementType IN (1838, 1839)
						          AND sr.ResourceType = 101
							)src
							ORDER BY 1;
				
				        OPEN crs;
				
                END;
				
	END;

	
END ss	



