DECLARE @LogDate AS BIGINT = dbo.GetUnixTime(GETUTCDATE()) 
DECLARE @queryId AS INTEGER = 10354
DECLARE @surveyXML nvarchar(MAX) 
 
DECLARE @releaseId integer 
SET @releaseId = (select releaseId from APP_Client where id = 2) 
 
DECLARE @temptbl table 
(
[NumOfJobs] int
)


IF @releaseId = 16
BEGIN 
 insert into @temptbl 
EXEC('SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SET NOCOUNT ON; SELECT COUNT(DISTINCT j.jobId) AS NumOfJobs
 FROM TM_JobXMLOptions t WITH(NOLOCK) JOIN JMQinetixUpdateStatus j WITH(NOLOCK) ON t.jobId=j.jobId
 JOIN simInstalledThirdPartyCU s WITH(NOLOCK) ON j.clientId = s.clientId AND s.eligible = 1 AND s.enabled = 1
 OUTER APPLY t.xmlValue.nodes(''TMMsg_JobOption/adminOpts/updateOption'') AS m(c)
 WHERE m.c.value(''@installUpdateOptions'', ''int'') > 0 ') 
 END
 
 SET @surveyXML = 
( SELECT 
(
SELECT [NumOfJobs] AS '@NumOfJobs' FROM @temptbl
 FOR XML PATH ('cf_MetricsQuery10354'), 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')) 
