/* Script recalculates SLA paused hours for all openned tickets with specific SLA */

-- Set changed SLA ID
declare @slaId int = 0

---------------------------------------------------

CREATE TABLE #ticketsToUpdate (id int PRIMARY KEY, processed int)

INSERT INTO #ticketsToUpdate
    SELECT iHdTicketId, 0
	FROM tHdTicket 
	WHERE liHdTicketSlaId = @slaId
		AND ClosedDate IS NULL

WHILE (EXISTS(SELECT 1 FROM #ticketsToUpdate WHERE processed = 0))
BEGIN
	DECLARE @id int;
	SELECT @id = id FROM #ticketsToUpdate WHERE processed = 0

	EXEC spUpdateSlaPausedHours @id

	UPDATE #ticketsToUpdate SET processed = 1 WHERE id = @id
END

DROP TABLE #ticketsToUpdate