I made the cursor below to update the contacts. It runs successfully, however it does not provide the expected result.
DECLARE @sequencia int,
@codigoOld int,
@telefoneOld nvarchar(5),
@timestampOld datetime,
@vetorOld int,
@codigoNew int,
@telefoneNew nvarchar(5),
@timestampNew datetime,
@vetorNew int,
@codigo INT ,
@telefone INT;
DECLARE CONFLITO_CURSOR CURSOR FOR SELECT sequencia,codigoOld,
telefoneOld,timestampOld,vetorOld,codigoNew,telefoneNew,timestampNew,
vetorNew FROM registoOperacoes ORDER BY sequencia ASC
OPEN CONFLITO_CURSOR
FETCH NEXT FROM CONFLITO_CURSOR INTO @sequencia,@codigoOld,@telefoneOld,
@timestampOld,@vetorOld,@codigoNew,@telefoneNew,@timestampNew,@vetorNew
WHILE (@@FETCH_STATUS=0)
begin
DECLARE SERVIDOR_CURSOR CURSOR FOR SELECT codigo,telefone FROM servidor , registoOperacoes WHERE servidor.codigo=@codigoNew
OPEN SERVIDOR_CURSOR
FETCH NEXT FROM SERVIDOR_CURSOR INTO @codigo,@telefone
WHILE (@@FETCH_STATUS=0)
BEGIN
IF @timestampOld<@timestampNew
print N'Código: '+CAST(@codigo AS NVARCHAR(10))+' Telefone: '+CAST(@telefone AS NVARCHAR(15));
UPDATE servidor
SET codigo=@codigo,telefone=@telefone
WHERE codigo=@codigo
FETCH NEXT FROM SERVIDOR_CURSOR INTO @codigo,@telefone
END
CLOSE SERVIDOR_CURSOR
DEALLOCATE SERVIDOR_CURSOR
FETCH NEXT FROM CONFLITO_CURSOR INTO @sequencia,@codigoOld,@telefoneOld,@timestampOld,@vetorOld,@codigoNew,@telefoneNew,@timestampNew,@vetorNew
END
CLOSE CONFLITO_CURSOR
DEALLOCATE CONFLITO_CURSOR