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
       (
        ResourceID INTEGER ,
		ResourceType INTEGER ,
		Description VARCHAR(50) ,
		StatementStartRow INTEGER ,
		StatementStartColumn INTEGER ,
		StatementEndRow INTEGER ,
		StatementEndColumn INTEGER ,
		StmtPathStr VARCHAR(250) ,
		ProgramName VARCHAR(256) ,
		ProgramId INTEGER ,
		ProgramTypeID INTEGER ,
		ProgramPath VARCHAR(250) ,
		Ancestor VARCHAR(256) ,
		ProgStartRow INTEGER ,
		ProgStartCol INTEGER ,
		ProgEndRow INTEGER ,
		ProgEndCol INTEGER 
        ) ON COMMIT PRESERVE ROWS
          NOT LOGGED;
  
	CREATE INDEX core_IX_1 ON SESSION.core (ResourceType ASC, ResourceID ASC) CLUSTER;   
						

    -- fill in temp table
    INSERT INTO SESSION.core
	SELECT sr.ResourceID,
	       sr.ResourceType,
	       st.Description,
	       os.StartRow AS StatementStartRow,
	       os.StartCol AS StatementStartColumn,
	       os.EndRow AS StatementEndRow,
	       os.EndCol AS StatementEndColumn,
	       Paths_Stmt.PathStr AS StmtPathStr,
	       prg.ProgramName,
	       prg.ProgramID AS ProgramId,
	       prg.ProgramTypeID,
	       pth.PathStr AS ProgramPath,
	       prg.Ancestor AS Ancestor,
	       occ.StartRow AS ProgStartRow,
	       occ.StartCol AS ProgStartCol,
	       occ.EndRow AS ProgEndRow,
	       occ.EndCol AS ProgEndCol
	FROM  StatementReference sr 
	      INNER JOIN OccurrencesStmt os ON os.OccurID = sr.OccurID
	      INNER JOIN Paths AS Paths_Stmt ON Paths_Stmt.PathID = os.PathID
	      INNER JOIN 
                    (   
                         SELECT p.ProgramID,
			                    p.Ancestor,
					            p.ProgramTypeID,
					            p.OccurID,
			                    pa.AliasName AS ProgramName
			              FROM Programs p
			                   INNER JOIN ProgramAliases pa ON p.ProgramID = pa.ProgramId AND pa.AliasType = 0
			          ) prg ON os.ProgID = prg.ProgramID
	      INNER JOIN Statements st ON sr.StatementType = st.StatementType
	      INNER JOIN Occurrences occ ON occ.OccurID = prg.OccurID
	      INNER JOIN Paths pth ON pth.PathID = occ.PathID
	WHERE sr.ResourceType IN (79, 81);
    COMMIT;
                        

    BEGIN
      COMMIT; 
      TRUNCATE TABLE cacheEZViewer_Usage_MQ ;
    END;
      

    LOCK TABLE  cacheEZViewer_Usage_MQ IN EXCLUSIVE MODE ;-- lock table 
	INSERT INTO cacheEZViewer_Usage_MQ
	SELECT  QueueId, QueueName, QueueManagerID, QueueManagerName,
	        Description, StatementStartRow, StatementStartColumn, StatementEndRow, StatementEndColumn, StmtPathStr,
	        ProgramName, ProgramId, ProgramTypeID, ProgramPath, Ancestor, ProgStartRow, ProgStartCol, ProgEndRow, ProgEndCol
	FROM (
		   SELECT   mq.QueueID AS QueueId,
					mq.QueueName AS QueueName,
					mm.QueueManagerID,
					mm.QueueManagerName,
					c.Description,
					c.StatementStartRow,
					c.StatementStartColumn,
					c.StatementEndRow,
					c.StatementEndColumn,
					c.StmtPathStr,
					c.ProgramName,
					c.ProgramId,
					c.ProgramTypeID,
					c.ProgramPath,
					c.Ancestor,
					c.ProgStartRow,
					c.ProgStartCol,
					c.ProgEndRow,
					c.ProgEndCol
		   FROM MQQueues mq
			   INNER JOIN MQQueueManagers mm ON mm.QueueManagerID = mq.QueueManagerID
			   INNER JOIN SESSION.core c ON c.ResourceType = 81 AND c.ResourceID = mq.QueueID
		   WHERE mq.QueueID <> -1
	
		   UNION
	
		   SELECT 
				0 AS QueueId,--'' AS QueueId,
				'Queue Manager Operations' AS QueueName,
				mm.QueueManagerID,
				mm.QueueManagerName,
				c.Description,
				c.StatementStartRow,
				c.StatementStartColumn,
				c.StatementEndRow,
				c.StatementEndColumn,
				c.StmtPathStr,
				c.ProgramName,
				c.ProgramId,
				c.ProgramTypeID,
				c.ProgramPath,
				c.Ancestor,
				c.ProgStartRow,
				c.ProgStartCol,
				c.ProgEndRow,
				c.ProgEndCol
		   FROM  MQQueueManagers mm
			    INNER JOIN SESSION.core c ON c.ResourceType = 79 AND c.ResourceID = mm.QueueManagerID
		   WHERE mm.QueueManagerID <> -1
		  ) s;
		  COMMIT;
   
END 