LANGUAGE SQL
sp:BEGIN   

	BEGIN
      COMMIT; 
      TRUNCATE TABLE cacheEZViewer_Usage_SQLTables ;
    END;


	 LOCK TABLE  cacheEZViewer_Usage_SQLTables IN EXCLUSIVE MODE ;-- lock table 
	 INSERT INTO cacheEZViewer_Usage_SQLTables 
	 SELECT 
		   c.SqlTableID,
		   c.TableName,
		   c.Description,
		   c.StatementStartRow,
		   c.StatementStartColumn,
		   c.StatementEndRow,
		   c.StatementEndColumn,
		   pth1.PathStr AS StmtPathStr,
		   c.ProgramName,
		   c.ProgramID,
		   c.ProgramTypeID,
		   pth2.PathStr AS ProgramPath,
		   c.ProgStartRow,
		   c.ProgStartCol,
		   c.ProgEndRow,
		   c.ProgEndCol,
		   c.Ancestor,
		   c.AncestorID,
		   c.AncestorTypeID 
	     FROM
				    (   SELECT 
						   core.SqlTableID,
						   core.TableName,
						   core.Description,
						   core.StartRow AS StatementStartRow,
						   core.StartCol AS StatementStartColumn,
						   core.EndRow AS StatementEndRow,
						   core.EndCol AS StatementEndColumn,
						   core.PathID AS StmtPathID,
						   prog.ProgramName,
						   prog.ProgramID,
						   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 (  
								 SELECT  
									    os.ProgID,
									    os.PathID,
									    os.StartRow,
									    os.StartCol,
									    os.EndRow,
									    os.EndCol,
									    sr.ResourceID,
									    st.Description,
									    res.SqlTableID,
						                   res.TableName 
								 FROM StatementReference sr
									  INNER JOIN OccurrencesStmt os ON sr.OccurID = os.OccurID
									  INNER JOIN Statements st ON sr.StatementType = st.StatementType
									  INNER JOIN SqlTables res ON res.SqlTableID = sr.ResourceID AND res.szDataBase = '1' 
								 WHERE sr.ResourceType = 1
									   AND  os.PathID IS NOT NULL
							    )core
								INNER JOIN ( -- programs
											SELECT p.ProgramID, p.ProgramName, p.ProgramTypeID, p.Ancestor,
												  occ.StartRow, occ.StartCol, occ.EndRow, occ.EndCol
												 ,occ.PathID
												 ,pa.ProgramID AS AncestorID, pa.ProgramTypeID AS AncestorTypeID
											   FROM Occurrences occ
													INNER JOIN 
															  (
																SELECT tp.ProgramID,
																	   tp.ProgramTypeID,
																	   tpa.AliasName AS ProgramName,
																	   tp.Ancestor,
																	   UPPER(tp.Ancestor) AS AncestorU,
																	   tp.OccurId
																FROM Programs tp
																	 INNER JOIN ProgramAliases tpa ON tp.ProgramID = tpa.ProgramID AND tpa.AliasType = 0
																WHERE tp.ProgramTypeID IN (1,2,15,16)	 
															  ) p ON p.OccurID = occ.OccurID 
												    LEFT OUTER JOIN 
													               (
																	SELECT DISTINCT
																	       tp.ProgramID,
																		   tp.ProgramTypeID,
																		   --tpa.AliasName AS ProgramName,
																		   UPPER(tpa.AliasName) AS ProgramNameU
																		   --tp.Ancestor,
																		   --tp.OccurId
																	FROM Programs tp
																		 INNER JOIN ProgramAliases tpa ON tp.ProgramID = tpa.ProgramID 
																	WHERE tp.ProgramTypeID = 8	 
															        ) pa ON  pa.ProgramNameU = p.AncestorU
											   WHERE occ.PathID IS NOT NULL
										 ) prog ON core.ProgID = prog.ProgramID  
							   GROUP BY core.SqlTableID, core.TableName, core.Description,
									  core.StartRow , core.StartCol, core.EndRow, core.EndCol, core.PathID,
									  prog.ProgramName, prog.ProgramID,  prog.ProgramTypeID, prog.PathID,  prog.StartRow, prog.StartCol, prog.EndRow, prog.EndCol, prog.Ancestor,
									  prog.AncestorID, prog.AncestorTypeID
				  
			  )c
			   LEFT OUTER JOIN  Paths pth1 ON pth1.PathID = c.StmtPathID
			   LEFT OUTER JOIN  Paths pth2 ON pth2.PathID = c.ProgramPathID ; 
			   COMMIT;

END 