LANGUAGE SQL
sp:BEGIN   

    -- clean-up temporary tables / SESSION tables
	BEGIN
		DECLARE CONTINUE HANDLER FOR SQLSTATE '42704'
		BEGIN END;
		
		COMMIT;
		DROP TABLE SESSION.core;
		DROP INDEX SESSION.core_IX_1;
	END;

   -- declare temp table 
   DECLARE GLOBAL TEMPORARY TABLE SESSION.core
   (
	ProgId INTEGER ,
	PathID INTEGER ,
	StartRow INTEGER ,
	StartCol INTEGER ,
	EndRow INTEGER ,
	EndCol INTEGER ,
	Description VARCHAR(50) 
    ) ON COMMIT PRESERVE ROWS
      NOT LOGGED;

	CREATE INDEX core_IX_1 ON SESSION.core (ProgId ASC) CLUSTER;  


    -- fill in temp table
    INSERT INTO SESSION.core
    SELECT  
		   os.ProgID,
		   os.PathID,
		   os.StartRow,
		   os.StartCol,
		   os.EndRow,
		   os.EndCol,
		   st.Description
	FROM  OccurrencesStmt os 
		 INNER JOIN Statements st ON os.StatementType = st.StatementType
	WHERE st.StatementType BETWEEN 1845 AND 1855
		  AND  os.PathID IS NOT NULL ;
	COMMIT;
		  

    BEGIN
      COMMIT; 
      TRUNCATE TABLE cacheEZViewer_Usage_OpenTP ;
    END;
      
    LOCK TABLE  cacheEZViewer_Usage_OpenTP IN EXCLUSIVE MODE ;-- lock table 
	INSERT INTO cacheEZViewer_Usage_OpenTP 
	SELECT 
		   Description, 
		   StatementStartRow, StatementStartColumn, StatementEndRow, StatementEndColumn, 
		   pth1.PathStr AS StmtPathStr,
		   ProgramID, ProgramName, ProgramTypeID, 
		   pth2.PathStr AS ProgramPath, ProgStartRow, ProgStartCol, ProgEndRow, ProgEndCol, Ancestor, AncestorID, AncestorTypeID
	FROM (
			  SELECT DISTINCT
				    core.Description,
				    core.PathID AS StmtPathID,
				    core.StartRow AS StatementStartRow,
				    core.StartCol AS StatementStartColumn,
				    core.EndRow AS StatementEndRow,
				    core.EndCol AS StatementEndColumn,
				    prog.ProgramID,
				    prog.ProgramName,
				    prog.ProgramTypeID,
				    prog.PathID AS ProgramPathID,
				    prog.StartRow AS ProgStartRow,
				    prog.StartCol AS ProgStartCol,
				    prog.EndRow AS ProgEndRow,
				    prog.EndCol AS ProgEndCol,
				    prog.Ancestor,
				    prog.AncestorID,
				    prog.AncestorTypeID
			  FROM  SESSION.core core
				    INNER JOIN ( -- programs
							    SELECT p.ProgramID, p.ProgramName, p.ProgramTypeID, p.Ancestor,
							           ap.ProgramID AS AncestorID , ap.ProgramTypeID AS AncestorTypeID, 
									   occ.StartRow, occ.StartCol, occ.EndRow, occ.EndCol
									   ,occ.PathID
								  FROM (   
											 SELECT pg.ProgramID,
											        pg.Ancestor,
											        UPPER(pg.Ancestor) AS AncestorU,
										            pg.ProgramTypeID,
										            pg.OccurID,
											        pa.AliasName AS ProgramName
											  FROM Programs pg
												   INNER JOIN ProgramAliases pa ON pg.ProgramID = pa.ProgramId AND pa.AliasType = 0
											 WHERE EXISTS (SELECT 1 FROM SESSION.core c WHERE pg.ProgramID = c.ProgID)   
										 ) p   
									   INNER JOIN Occurrences occ ON p.OccurID = occ.OccurID
									   LEFT OUTER JOIN(   
														 SELECT DISTINCT
														        pg.ProgramID,
														        --pg.Ancestor,
													            pg.ProgramTypeID,
													            --pg.OccurID,
														        UPPER(pa.AliasName) AS ProgramNameU
														  FROM Programs pg
															   INNER JOIN ProgramAliases pa ON pg.ProgramID = pa.ProgramId
															   WHERE pg.ProgramTypeID IN ( 8, 13) 
													  ) ap ON p.AncestorU =  ap.ProgramNameU 
								  WHERE occ.PathID IS NOT NULL
							) prog ON core.ProgID = prog.ProgramID  
			  )c
			   LEFT OUTER JOIN  Paths pth1 ON pth1.PathID = c.StmtPathID
			   LEFT OUTER JOIN  Paths pth2 ON pth2.PathID = c.ProgramPathID	;
			   
	    COMMIT ;	
  
END 