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


IF @releaseId = 16
BEGIN 
 insert into @temptbl 
EXEC('SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SET NOCOUNT ON; select count( distinct S.SIDBStoreId) as numStoresToUpgrade
from IdxSIDBSubStore S WITH (NOLOCK), IdxSIDBStore I WITH (NOLOCK), archCopySIDBStore CS WITH (NOLOCK)
where S.SIDBStoreId = I.SIDBStoreId
AND I.SIDBStoreId = CS.SIDBStoreId
AND S.extendedFlags & 1 = 0
AND I.flags & 256 = 0
AND I.SealedTime = 0 ') 
 END
 
 SET @surveyXML = 
( SELECT 
(
SELECT [numStoresToUpgrade] AS '@numStoresToUpgrade' FROM @temptbl
 FOR XML PATH ('cf_MetricsQuery10319'), 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')) 
