DECLARE @LogDate AS BIGINT = dbo.GetUnixTime(GETUTCDATE()) 
DECLARE @queryId AS INTEGER = 10361
DECLARE @surveyXML nvarchar(MAX) 
 
DECLARE @temptbl table 
(
[appType] int,[opType] int,[opName] nvarchar(max),[phase] int,[Phase Name] nvarchar(max)
)
BEGIN 
 insert into @temptbl 
EXEC('SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SET NOCOUNT ON; Set transaction isolation level read uncommitted
select distinct JB.appType, JB.opType , JMO.opName, JBA.phase, JMP.name AS "Phase Name"  from JMBkpAtmptStats JBA, JMBkpStats JB,  JMFailureReasonMsg JFR, JMOpTable JMO, JMPhase JMP
where JBA.status not in (1, 3, 14) 
and JB.jobId = JBA.jobId
and JB.jobId = JFR.jobId
and JFR.messageId = 318768213
and (JBA.phase = JFR.phaseNumber OR JFR.phaseNumber = 0)
and JB.appType = JMO.appTypeType
and JMO.operation = JB.opType
and JMO.id = JMP.opTableId
and JBA.phase = JMP.phase ') 
 END
 
 SET @surveyXML = 
( SELECT 
(
SELECT [appType] AS '@appType',[opType] AS '@opType',[opName] AS '@opName',[phase] AS '@phase',[Phase Name] AS '@Col_1623221840' FROM @temptbl
 FOR XML PATH ('cf_MetricsQuery10361'), type
)
  FOR XML PATH ('SurveyResults') 
)

DECLARE @EndTime AS BIGINT = dbo.GetUnixTime(GETUTCDATE()) 
SET @outputXML = (SELECT @queryId AS '@QueryId', @EndTime AS '@LogDate', (@EndTime - @LogDate) AS '@QueryRunningTime', @surveyXML FOR XML PATH('Rpt_CSSXMLDATA')) 
