LANGUAGE SQL
sp:BEGIN   


			
	            BEGIN
			      COMMIT; 
	              TRUNCATE TABLE cacheEZViewer_Usage_FileInPrg ;
	            END;


				INSERT INTO  cacheEZViewer_Usage_FileInPrg	           
				SELECT   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 IS NOT NULL
							   )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 DISTINCT
																	        pg.ProgramID,
																			--pg.Ancestor,
																			pg.ProgramTypeID,
																			--pg.OccurID,
																			px.AliasName AS ProgramName
																	  FROM Programs pg
																		   INNER JOIN ProgramAliases px ON pg.ProgramID = px.ProgramId																	   
																   ) pa ON UPPER(pa.ProgramName) = UPPER(p.Ancestor) AND pa.ProgramTypeID = 13  
											  WHERE occ.PathID IS NOT NULL
										) prog ON core.ProgID = prog.ProgramID  
							    LEFT OUTER JOIN SCLSystemFiles AS FilesForSCL ON core.FileOccurID = FilesForSCL.OccurID
				                                                                       AND ((core.FileID = FilesForSCL.fileId AND prog.ProgramTypeID IN (13, 19))
				                                                                              OR 
																			                 (core.FileName = FilesForSCL.SystemFileId AND prog.ProgramTypeID = 1))
							    LEFT OUTER JOIN SCLSystemFiles sf ON core.FileID = sf.fileId AND prog.ProgramTypeID IN (13, 19)
							    LEFT OUTER JOIN SCLSystemFiles AS SCLSystemFiles1 ON core.FileName = SCLSystemFiles1.SystemFileId AND prog.ProgramTypeID = 1
							  GROUP BY  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, 
								       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 
						  
						  )c
						   LEFT OUTER JOIN  Paths pth1 ON pth1.PathID = c.StmtPathID
						   LEFT OUTER JOIN  Paths pth2 ON pth2.PathID = c.ProgramPathID	;					
END 






