DYNAMIC RESULT SETS 1
LANGUAGE SQL
 BEGIN

            DECLARE crs CURSOR WITH RETURN FOR
		    SELECT  res.Name AS DBITable,
		            acc.pattern AS DBIPattern,
		            pa.AliasName AS ProgramName,
		            st.Description,
		            COUNT(os.OccurID) AS stmtCount,
					pth.PathStr
		     FROM StatementReference sr
		            INNER JOIN OccurrencesStmt os ON sr.OccurID = os.OccurID
		            INNER JOIN Statements st ON os.StatementType = st.StatementType
		            INNER JOIN Programs prg ON os.ProgID = prg.ProgramID
					INNER JOIN ProgramAliases pa ON prg.ProgramID = pa.ProgramId AND pa.AliasType = 0
		            INNER JOIN Resources res ON sr.ResourceID = res.ResourceID AND sr.ResourceType = 190
		            LEFT OUTER JOIN
								     (
								         SELECT ar.Name AS pattern, asr.occurid AS occid
								         FROM StatementReference asr
								              INNER JOIN Resources ar ON asr.ResourceID = ar.ResourceID AND asr.ResourceType = 191
								     ) AS acc ON acc.occid = os.OccurID
                     LEFT OUTER JOIN
									 (
										SELECT   1 AS Flag,
												pgx.ProgramName, pgx.ProgramTypeID
										 FROM Programs pgx
										 GROUP BY pgx.ProgramName, pgx.ProgramTypeID
										 HAVING COUNT(*) > 1
									 ) x ON prg.ProgramName = x.ProgramName AND prg.ProgramTypeID = x.ProgramTypeID
					 LEFT OUTER JOIN Occurrences occ ON prg.OccurID = occ.OccurID				
					 LEFT OUTER JOIN Paths pth ON pth.PathID = occ.PathID AND x.Flag = 1
		     WHERE  prg.ProgramTypeID = 1
		     GROUP BY res.Name, acc.pattern, pa.AliasName, st.Description, pth.PathStr
		     ORDER BY res.Name, acc.pattern, pa.AliasName, pth.PathStr, st.Description;

            OPEN crs;

 END	
