LANGUAGE SQL
BEGIN

			-- declare temp tables
	                   DECLARE GLOBAL TEMPORARY TABLE session.prg
		               (
					    ProgramId INTEGER,
						PathStr VARCHAR(250)
		                ) WITH REPLACE 
                          ON COMMIT PRESERVE ROWS
						  NOT LOGGED;
			
			
			-- fill in temp table
						  INSERT INTO session.prg
						  SELECT p.ProgramId, pth.PathStr
						  FROM  Programs p
						        INNER JOIN Occurrences occ ON p.OccurID = occ.OccurID
						        INNER JOIN Paths pth ON occ.PathID = pth.PathID ;
			              COMMIT;
			
			
			
			
				            BEGIN
						      COMMIT;
							  TRUNCATE TABLE cacheEZViewer_Usage_FilesInDDCL IMMEDIATE ;
				              --DELETE FROM cacheEZViewer_Usage_FilesInDDCL;
				            END;

					


                             -- fill in cache table
                            LOCK TABLE  cacheEZViewer_Usage_FilesInDDCL IN EXCLUSIVE MODE ;-- lock table
							INSERT INTO cacheEZViewer_Usage_FilesInDDCL
							SELECT FileName, FileID, FileStartRow, FileEndRow, FileStartCol, FileEndCol, ddclfilePath, ddclElemName,
							       Id, StartRow, EndRow, StartCol, EndCol, Description, DdsTypeId, ddclPath,
								  PropertyName, PropertyID, PropertyPath, PropertyStartRow, PropertyStartCol, PropertyEndRow, PropertyEndCol
							FROM
							(
							    SELECT dv.referencename AS FileName,
									 DDCLElement_1.Id AS FileID,
									 DDCLFileInfo.StartRow AS FileStartRow,
									 DDCLFileInfo.EndRow AS FileEndRow,
									 DDCLFileInfo.StartCol AS FileStartCol,
									 DDCLFileInfo.EndCol AS FileEndCol,
									 PrgForDDCLFile.PathStr AS ddclfilePath,
									 de.name AS ddclElemName,
									 de.Id,
									 DDCLElementInfo.StartRow,
									 DDCLElementInfo.EndRow,
									 DDCLElementInfo.StartCol,
									 DDCLElementInfo.EndCol,
									 DDCLElemType.Description,
									 de.DdsTypeId,
									 PrgForDDCLElem.PathStr AS ddclPath,
									 dp.name AS PropertyName,
									 dp.Id AS PropertyID,
									 PrgForDDCLElem.PathStr AS PropertyPath,
									 PropertySourceInfo.StartRow AS PropertyStartRow,
									 PropertySourceInfo.StartCol AS PropertyStartCol,
									 PropertySourceInfo.EndRow AS PropertyEndRow,
									 PropertySourceInfo.EndCol AS PropertyEndCol
							    FROM DDCLElement de
								    INNER JOIN DDCLProperty dp ON de.id = dp.elementid
								    INNER JOIN DDCLValue dv ON dp.id = dv.propertyid
								    INNER JOIN DDCLSourceInfo AS DDCLElementInfo ON de.SourceInfo = DDCLElementInfo.Id
								    INNER JOIN DDCLDdsType AS DDCLElemType ON DDCLElemType.Id = de.DdsTypeId
								    LEFT OUTER JOIN DDCLSourceInfo AS PropertySourceInfo ON dp.SourceInfo = PropertySourceInfo.Id
								    LEFT OUTER JOIN DDCLElement AS DDCLElement_1 ON DDCLElement_1.DdsTypeId = dv.referenceddstypeid
																			 AND dv.ReferenceName = DDCLElement_1.Name
								    LEFT OUTER JOIN DDCLSourceInfo AS DDCLFileInfo ON DDCLElement_1.SourceInfo = DDCLFileInfo.Id
								    LEFT OUTER JOIN session.prg AS PrgForDDCLFile ON DDCLFileInfo.ProgramId = PrgForDDCLFile.ProgramID
								    LEFT OUTER JOIN session.prg AS PrgForDDCLElem ON DDCLElementInfo.ProgramId = PrgForDDCLElem.ProgramID
							    WHERE de.islocal = 0--'false'
									AND de.DdsTypeId IN (27, 28)
									AND dv.referenceddstypeid = 12
									AND dv.ValueTypeId IN (2, 4)
							)s;
							COMMIT;


			
END 
