DECLARE @LogDate AS BIGINT = dbo.GetUnixTime(GETUTCDATE()) 
DECLARE @queryId AS INTEGER = 10313
DECLARE @surveyXML nvarchar(MAX) 
 
DECLARE @releaseId integer 
SET @releaseId = (select releaseId from APP_Client where id = 2) 
 
DECLARE @temptbl table 
(
[Total DDB] int,[MemDB] int,[Active MemDB] int,[Initialized Active MemDB] int,[HasActiveMemDB] int,[HasInitedActiveMemDB] int
)


IF @releaseId = 16
BEGIN 
 insert into @temptbl 
EXEC('SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SET NOCOUNT ON; SELECT  Total [Total DDB], MemDB [MemDB], ActiveMemDB [Active MemDB], InitedActiveMemDB [Initialized Active MemDB],HasActiveMemDB, HasInitedActiveMemDB 
FROM	(	SELECT	SUM(1) Total,
					SUM((CASE WHEN flags&8192 <> 0 AND SealedTime = 0 AND ACSS.SIDBStoreId IS NOT NULL THEN 1 ELSE 0 END)) ActiveMemDB,
					MAX((CASE WHEN flags&8192 <> 0 AND SealedTime = 0 AND ACSS.SIDBStoreId IS NOT NULL THEN 1 ELSE 0 END)) HasActiveMemDB,
					SUM((CASE WHEN flags&8192 <> 0 AND SealedTime = 0 AND FirstBackupTime <> -1 THEN 1 ELSE 0 END)) InitedActiveMemDB,
					MAX((CASE WHEN flags&8192 <> 0 AND SealedTime = 0 AND FirstBackupTime <> -1 THEN 1 ELSE 0 END)) HasInitedActiveMemDB,
					SUM((CASE WHEN flags&8192 <> 0 THEN 1 ELSE 0 END)) MemDB
			FROM	IdxSIDBStore SS (READUNCOMMITTED)
					LEFT JOIN (SELECT DISTINCT SIDBStoreId FROM archCopySIDBStore (READUNCOMMITTED)) ACSS ON ACSS.SIDBStoreId = SS.SIDBStoreId
		) T
WHERE	EXISTS (SELECT TOP 1 1 FROM IdxSIDBStore (READUNCOMMITTED) WHERE flags&8192 <> 0) ') 
 END
 
 SET @surveyXML = 
( SELECT 
(
SELECT [Total DDB] AS '@Col_1425099974',[MemDB] AS '@MemDB',[Active MemDB] AS '@Col_93103993',[Initialized Active MemDB] AS '@Col_-1783190171',[HasActiveMemDB] AS '@HasActiveMemDB',[HasInitedActiveMemDB] AS '@HasInitedActiveMemDB' FROM @temptbl
 FOR XML PATH ('cf_MetricsQuery10313'), 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')) 
