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
			;

	            BEGIN

					    DECLARE crs CURSOR WITH RETURN FOR
					     SELECT  par.ParaID,
								 par.ParaName,
								 gpf.StatementType,
								 gpf.LogicalName,
								 gpf.FileId,
								 gpf.StartRow0,
								 gpf.EndRow0,
								 gpf.StartCol0,
								 gpf.EndCol0,
								 gpf.PathStr,
								 pfd.PrinterFileName,
								 32,
								 gpf.ProgID
					     FROM PrinterFileDDS pfd
					           LEFT OUTER JOIN Occurrences occ ON pfd.OccurId = occ.OccurID
					           LEFT OUTER JOIN
										    (
											  ---//#################################
											  --- query from GetPrinterFiles view
					
											   SELECT f.LogicalName,
													st.Description,
													os.StartRow AS StartRow0,
													os.StartCol AS StartCol0,
													os.EndRow AS EndRow0,
													os.EndCol AS EndCol0,
													pth.PathStr,
													prg.ProgramName,
													f.Name,
													f.ProgID AS ProgID,
													os.StatementType AS StatementType,
													f.FileID AS FileId,
													os.ParaID AS occstmtparaid
											   FROM SESSION.programWithAlias prg
												   INNER JOIN OccurrencesStmt os ON prg.ProgramID = os.ProgID
												   INNER JOIN StatementReference sr ON sr.OccurID = os.OccurID
												   INNER JOIN Statements st ON st.StatementType = os.StatementType
												   INNER JOIN Files f ON sr.ResourceID = f.FileID
												   INNER JOIN Paths pth ON os.PathID = pth.PathID				
											   WHERE f.ProgID = 0
												    AND (f.TypeCode IS NULL OR f.TypeCode = 32)
												    AND sr.ResourceType = 9
												    AND prg.ProgramTypeID = 1
												    AND prg.ProgramName IN (SELECT * FROM SESSION.string_param_temp)
												
					
											   UNION
					
											   SELECT f.LogicalName,
													st.Description,
													os.StartRow AS StartRow0,
													os.StartCol AS StartCol0,
													os.EndRow AS EndRow0,
													os.EndCol AS EndCol0,
													pth.PathStr,
													prg.ProgramName,
													f.Name,
													f.ProgID AS ProgID,
													os.StatementType AS StatementType,
													f.FileID AS FileId,
													os.ParaID AS occstmtparaid
											   FROM SESSION.programWithAlias prg
											       INNER JOIN Files f ON prg.ProgramID = f.ProgID
												   INNER JOIN StatementReference sr ON sr.ResourceID = f.FileID
												   INNER JOIN OccurrencesStmt os ON sr.OccurID = os.OccurID
												   INNER JOIN Statements st ON st.StatementType = os.StatementType
												   INNER JOIN Paths pth ON os.PathID = pth.PathID
											   WHERE (f.TypeCode IS NULL OR f.TypeCode = 32)
												    AND sr.ResourceType = 9
												    AND prg.ProgramTypeID = 1
												     AND prg.ProgramName IN (SELECT * FROM SESSION.string_param_temp)
											  ---//################################
										    ) AS gpf ON pfd.PrinterFileName = gpf.Name
					                                                   OR
					                                                   gpf.Name LIKE '%' || '-' || pfd.PrinterFileName
					                                                  OR
					                                                  gpf.Name LIKE pfd.PrinterFileName || '-' || '%'
					                                                  OR
					                                                  gpf.Name LIKE '%' || '-' || pfd.PrinterFileName || '-' ||'%'
					                 INNER JOIN Paragraphs par ON gpf.occstmtparaid = par.ParaID
					     ORDER BY gpf.ProgramName,
					              pfd.PrinterFileName;
						

				        OPEN crs;
				
                END;
				
	END;

	
END	
