USE [ActivePos_server]; GO /****** Object: StoredProcedure [dbo].[GetSubscriptionStatus] Script Date: 09.11.2023 10:36:06 ******/ SET ANSI_NULLS ON; GO SET QUOTED_IDENTIFIER ON; GO /*========================================================= * Check the Subscription status for the given POS name * 0 = Inactive. 1 = Subscribed. 2 = Active. 99 = Not exist * 25.11.2020: SPA: TFS 61644 : * 27.01.2023: SPE: OCTPDBA-478: Resolve SQL 2019 compatibility issue when getting SQL replication subscription info * 03.11.2023: TSC: TPDT-133 Check if a subscription is running to be considered ok before other checks *========================================================*/ ALTER PROCEDURE [dbo].[GetSubscriptionStatus] @posName VARCHAR(255) AS BEGIN DECLARE @subscriptionStatus AS INT = 0; DECLARE @tmpStatus INT; SELECT @tmpStatus = MAX([h].[runstatus]) FROM [distribution].dbo.[MSdistribution_history] h JOIN [distribution].dbo.[MSdistribution_agents] a ON [a].[id] = [h].[agent_id] JOIN msdb.[dbo].[sysjobs] j ON j.[job_id] = a.[job_id] WHERE j.[name] LIKE '%' + @posName + '%'; PRINT 'repli execution status:' + CAST(@tmpStatus AS VARCHAR(3)); /* RunStatus meaning: 0 means something is wrong, should not be in that state. uninitialized subscriber are reported with this status 1 means 'Starting'. Agent is starting 2 means 'Succeeded'. Last action (starting or stopping) was succesful 3 means 'In Progress'. Agent is applying transactions 4 means 'Idle'. Agent is waiting for transactions to apply 5 means 'Retrying'. There was an error applying transactions, retrying is hapening now 6 means 'Failure'. Something went wrong while applying transactions, look in the history for details */ /* Expected subscription status return code: 99 = subscription not set / not existing 0 = subscription not valid (in error) 1 = subscribed 2 = active 3 = dev machine or pharmacy, no subscription checking done */ SELECT @subscriptionStatus = CASE WHEN @tmpStatus IS NULL THEN 99 WHEN @posName = SERVERPROPERTY('MachineName') THEN 3 WHEN @tmpStatus > 6 THEN 0 WHEN @tmpStatus IN ( 0, 6 ) THEN 0 WHEN EXISTS (SELECT 1 FROM distribution.sys.tables WHERE name = 'msreplservers') THEN ( SELECT TOP 1 [sub].[status] FROM distribution.dbo.MSsubscriptions sub INNER JOIN distribution.dbo.MSreplservers srv ON [sub].[subscriber_id] = [srv].[srvid] INNER JOIN distribution.dbo.MSarticles art ON art.article_id = sub.article_id WHERE [art].[article] = 'IIISales_order_header' AND [srv].[srvname] = @posName) WHEN NOT EXISTS (SELECT 1 FROM distribution.sys.tables WHERE name = 'msreplservers') THEN ( SELECT TOP 1 [sub].[status] FROM distribution.dbo.MSsubscriptions sub INNER JOIN master.sys.servers srv ON [sub].[subscriber_id] = [srv].[server_id] INNER JOIN distribution.dbo.MSarticles art ON art.article_id = sub.article_id WHERE [art].[article] = 'IIISales_order_header' AND [srv].[name] = @posName) ELSE 0 END; --IF @tmpStatus IS NULL --BEGIN -- /*99 = subscription not set*/ -- SET @subscriptionStatus = 99; --END --IF @tmpStatus NOT IN (1,2,3,4,5) --BEGIN -- /*0 = sub not valid*/ -- SET @subscriptionStatus = 0 --END --IF @tmpStatus > 0 AND @tmpStatus < 6 --BEGIN -- IF EXISTS ( SELECT * -- FROM distribution.sys.tables -- WHERE name = 'msreplservers') -- BEGIN -- SELECT TOP 1 @subscriptionStatus = [sub].[status] -- FROM distribution.dbo.MSsubscriptions sub -- INNER JOIN distribution.dbo.MSreplservers srv -- ON [sub].[subscriber_id] = [srv].[srvid] -- INNER JOIN distribution.dbo.MSarticles art -- ON art.article_id = sub.article_id -- WHERE [art].[article] = 'IIISales_order_header' -- AND [srv].[srvname] = @posName; -- END; -- ELSE -- BEGIN -- SELECT TOP 1 @subscriptionStatus = [sub].[status] -- FROM distribution.dbo.MSsubscriptions sub -- INNER JOIN master.sys.servers srv -- ON [sub].[subscriber_id] = [srv].[server_id] -- INNER JOIN distribution.dbo.MSarticles art -- ON art.article_id = sub.article_id -- WHERE [art].[article] = 'IIISales_order_header' -- AND [srv].[name] = @posName; -- END; --END SELECT @subscriptionStatus; END;