DECLARE @LogDate AS BIGINT = dbo.GetUnixTime(GETUTCDATE()) 
DECLARE @queryId AS INTEGER = 10397
DECLARE @surveyXML nvarchar(MAX) 
 
DECLARE @releaseId integer 
SET @releaseId = (select releaseId from APP_Client where id = 2) 
 
DECLARE @temptbl table 
(
[Number of WebServer(s) running old SQL server version] int
)


IF @releaseId = 16
BEGIN 
 insert into @temptbl 
EXEC('SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SET NOCOUNT ON; DECLARE @NumberofWebServersrunningoldSQLserverversion INT = 0
IF EXISTS (SELECT 1 FROM APP_Client (NOLOCK) WHERE id = 2 and releaseID >= 16) AND EXISTS (SELECT 1 FROM SimInstalledPackages (NOLOCK) WHERE clientId = 2 AND highestSP >=15)
select @NumberofWebServersrunningoldSQLserverversion= count(T.clientID)
from SimInstalledThirdpartyCU T (NOLOCK)
JOIN SimInstalledPackages P (NOLOCK) ON T.clientID = P.clientID and T.clientID > 2 and T.type=1 and P.simpackageID in (252, 1174) and LEN(updates) > 0 and CHARINDEX(''.'', updates) > 0 and SUBSTRING(updates, 0, CHARINDEX(''.'', updates)) < 12
select @NumberofWebServersrunningoldSQLserverversion AS ''Number of WebServer(s) running old SQL server version'' ') 
 END
 
 SET @surveyXML = 
( SELECT 
(
SELECT [Number of WebServer(s) running old SQL server version] AS '@Col_-1943687538' FROM @temptbl
 FOR XML PATH ('cf_MetricsQuery10397'), 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')) 
