LANGUAGE SQL
BEGIN

	            BEGIN
			      COMMIT;
				  TRUNCATE TABLE cacheEZViewer_Usage_FileInPrg IMMEDIATE ;
	              --DELETE FROM cacheEZViewer_Usage_FileInPrg ;
	            END;


				INSERT INTO  cacheEZViewer_Usage_FileInPrg	
				SELECT  DISTINCT
				         FileName, FileID, LogicalName,
				         ProgramID, ProgramName, ProgramTypeID,
				         pth2.PathStr AS ProgramPath, ProgStartRow, ProgStartCol, ProgEndRow, ProgEndCol,
					     Ancestor, AncestorID, AncestorTypeId,
					     Description, StatementStartRow, StatementStartColumn, StatementEndRow, StatementEndColumn, pth1.PathStr AS StmtPathStr
				FROM(
						  SELECT       CASE  WHEN prog.ProgramTypeID = 19
				                         THEN CASE
				                              WHEN FilesForSCL.Name IS NULL THEN core.FileName
				                              ELSE FilesForSCL.Name
				                              END
									  ELSE CASE
											 WHEN SCLSystemFiles1.Name IS NULL
											 THEN core.FileName
											 ELSE SCLSystemFiles1.Name
										  END
									  END AS FileName,
							    core.FileID,
							    core.LogicalName,
							    prog.ProgramID,
							    prog.ProgramName,
							    prog.ProgramTypeID,
							    prog.ProgramPathID,
							    prog.ProgStartRow,
							    prog.ProgStartCol,
							    prog.ProgEndRow,
							    prog.ProgEndCol,
							    prog.Ancestor,
							    prog.AncestorID,
							    prog.AncestorTypeID,
							    core.Description,
							    core.StatementStartRow,
							    core.StatementStartColumn,
							    core.StatementEndRow,
							    core.StatementEndColumn,
							    core.StmtPathID
						  FROM (
								SELECT  -- core data
									   os.ProgID,
									   os.PathID AS StmtPathID,
							             os.StartRow AS StatementStartRow,
							             os.StartCol AS StatementStartColumn,
							             os.EndRow AS StatementEndRow,
							             os.EndCol AS StatementEndColumn,
									   st.Description,
									   f.FileID,
									   f.Name AS FileName,
									   f.OccurID AS FileOccurID,
									   f.LogicalName
								FROM StatementReference sr
									 INNER JOIN OccurrencesStmt os ON sr.OccurID = os.OccurID
									 INNER JOIN Statements st ON sr.StatementType = st.StatementType
									 INNER JOIN Files f ON sr.ResourceID = f.FileID
								WHERE sr.ResourceType = 9
									  --AND  os.PathID > 0
							   )core
							    INNER JOIN ( -- programs
										      SELECT p.ProgramID, p.ProgramName, p.ProgramTypeID,
												 occ.StartRow AS ProgStartRow, occ.StartCol AS ProgStartCol, occ.EndRow AS ProgEndRow, occ.EndCol AS ProgEndCol,
												 occ.PathID AS ProgramPathID,
												 p.Ancestor, pa.ProgramID AS AncestorID, pa.ProgramTypeID AS AncestorTypeID
											  FROM Occurrences occ
												   INNER JOIN Programs p ON p.OccurID = occ.OccurID
				                                   LEFT OUTER JOIN
												                    (
																	 SELECT
																	        pg.ProgramID,
																			pg.ProgramTypeID,
																			px.AliasName AS ProgramName
																	  FROM Programs pg
																		   INNER JOIN ProgramAliases px ON pg.ProgramID = px.ProgramId	AND pg.ProgramTypeID = 13																
																   ) pa ON pa.ProgramName = p.Ancestor
											  --WHERE occ.PathID > 0
										) prog ON core.ProgID = prog.ProgramID
							    LEFT OUTER JOIN SCLSystemFiles AS FilesForSCL ON core.FileOccurID = FilesForSCL.OccurID
				                                                                       AND (
				                                                                              (core.FileName = FilesForSCL.SystemFileId AND prog.ProgramTypeID = 1)
				                                                                               OR
				                                                                              (core.FileID = FilesForSCL.fileId AND prog.ProgramTypeID IN (13, 19))
				                                                                            )
							    LEFT OUTER JOIN SCLSystemFiles AS SCLSystemFiles1 ON core.FileName = SCLSystemFiles1.SystemFileId AND prog.ProgramTypeID = 1
						  )c
						   LEFT OUTER JOIN  Paths pth1 ON pth1.PathID = c.StmtPathID
						   LEFT OUTER JOIN  Paths pth2 ON pth2.PathID = c.ProgramPathID	
						 WHERE pth1.PathID > 0 AND pth2.PathID > 0 ;
						 					
END 
