USE [ksubscribers]
GO
/****** Object:  StoredProcedure [dbo].[clearPendingProcedures]    Script Date: 7/14/2021 10:47:08 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



ALTER PROCEDURE [dbo].[clearPendingProcedures]

AS
BEGIN
declare 
@EventInstanceId numeric(18,0),
@EventScriptID int, 
@EventScriptIdx int, 
@EventAgentGUID numeric(26,0), 
@EventAgentName nvarchar(100)='', 
@UserName nvarchar(100)='',
@EventScriptSummary nvarchar(2500)='', 
@ErrorMessage nvarchar(500)='', 
@ScriptSkipped bit=1 



select @eventinstanceid = min(he.eventinstanceid)
from hermes.eventinstance he join scriptassignment sa on sa.eventinstanceid = he.eventinstanceid
where inprocess = 1


while @EventInstanceId is not null

begin 

select @EventScriptID = ocs.scriptid, @EventScriptIdx = sa.id, @EventAgentGUID = ocs.agentguid 
from orgcalendarschedule ocs 
join hermes.eventinstance ei on ocs.id = ei.orgcalendarscheduleid 
and ei.eventinstanceid = @eventinstanceid 
join scriptassignment sa on ocs.scriptid = sa.scriptid 
and ocs.agentguid = sa.agentguid 

exec Hermes.CompleteScriptEventInstance 
@EventInstanceID, @EventScriptID, @EventScriptIdx, @EventAgentGUID, @EventAgentName, @UserName, @EventScriptSummary,
@ErrorMessage, @ScriptSkipped 


select @eventinstanceid = min(he.eventinstanceid)
from hermes.eventinstance he join scriptassignment sa on sa.eventinstanceid = he.eventinstanceid
where inprocess = 1
 end 
 end

