Delete all agents from pending - OpsDB - SCOM

Sometimes agents get stuck in the pending state or in the agentactionpending table.

In order to delete them all here is a cursor that will do that automatically:

Please take a backup of the OpsDB before trying it out:)

declare @field1 nvarchar(50)

declare cur CURSOR LOCAL for
    select DisplayName from agentpendingaction

open cur

fetch next from cur into @field1

while @@FETCH_STATUS = 0 BEGIN

    --execute your sproc on each row
    exec p_AgentPendingActionDeleteByAgentName @field1

    fetch next from cur into @field1
END

close cur
deallocate cur