DECLARE @nAlertUID as uniqueidentifier; DECLARE @nContactUID as uniqueidentifier; DECLARE @nAlertRecipientUID as uniqueidentifier; SET @nAlertUID = (SELECT strAlertUID); IF @nAlertUID IS NOT NULL BEGIN -- Delete the existing recipients. UPDATE AlertRecipients SET lDeleted=-1 WHERE nAlertUID = @nAlertUID; -- Declare the cursor. DECLARE Contact_Cursor CURSOR FOR -- Define the select statement. SELECT DISTINCT nContactUID FROM FacilityContactLink WHERE lDeleted=0 AND lActive<>0 -- Iterate over the results. OPEN Contact_Cursor FETCH NEXT FROM Contact_Cursor INTO @nContactUID; WHILE @@FETCH_STATUS = 0 BEGIN -- Check if we have a record we can just 're-activate'. IF (SELECT COUNT(*) FROM AlertRecipients WHERE nAlertUID=@nAlertUID AND nContactUID=@nContactUID) > 0 BEGIN -- Get the first UID SET @nAlertRecipientUID = (SELECT TOP 1 nAlertRecipientUID FROM AlertRecipients WHERE nAlertUID=@nAlertUID AND nContactUID=@nContactUID); UPDATE AlertRecipients SET lActive=-1, lDeleted=0 WHERE nContactUID=@nContactUID AND nAlertUID=@nAlertUID AND nAlertRecipientUID=@nAlertRecipientUID; END ELSE BEGIN -- Inserting a new record. EXEC spl_AlertRecipients_INSERT @nContactUID, @nAlertUID, 'TO' END FETCH NEXT FROM Contact_Cursor INTO @nContactUID END CLOSE Contact_Cursor; DEALLOCATE Contact_Cursor; END ELSE BEGIN print 'The recall report does not exist.' END