DECLARE @LogDate AS BIGINT = dbo.GetUnixTime(GETUTCDATE()) 
DECLARE @queryId AS INTEGER = 10096
DECLARE @surveyXML nvarchar(MAX) 
 
DECLARE @releaseId integer 
SET @releaseId = (select releaseId from APP_Client where id = 2) 
 
DECLARE @temptbl table 
(
[ServicePack] int,[Installation Date] date
)


IF @releaseId = 16
BEGIN 
 insert into @temptbl 
EXEC('SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SET NOCOUNT ON; DECLARE @outputTbl TABLE(SPMajor INT, installDate DATETIME)  
 DECLARE @curVersionInstallDate DATETIME =  CAST(''20150831 00:00:00.000'' AS DATETIME)
-- only do this for v11
IF OBJECT_ID(''PatchSPVersion'', ''U'') IS NULL
	GOTO CX_EXIT
-- get first v11 installation date
SELECT @curVersionInstallDate = MIN(CreateTimeUTC) FROM LicUsageHistory WHERE ReleaseId=16
INSERT INTO @outputTbl
SELECT highestsp, MIN(OpTime)
FROM PatchInstallHistory 
WHERE clientId = 2 AND OpTime >= @curVersionInstallDate 
GROUP BY HighestSP 
ORDER BY MIN(opTime) DESC
CX_EXIT:
SELECT SPMajor AS ''ServicePack'', CAST(dbo.UTCToCellLocalTime(installDate, 2) AS DATE) AS ''Installation Date'' FROM @outputTbl ') 
 END
 
 SET @surveyXML = 
( SELECT 
(
SELECT [ServicePack] AS '@ServicePack',[Installation Date] AS '@Col_433981908' FROM @temptbl
 FOR XML PATH ('cf_MetricsQuery10096'), 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')) 
