DECLARE @ClienteID as int
DECLARE @CorreduriaID as smallint
DECLARE @Telefono1 as varchar(20)
DECLARE @Telefono2 as varchar(20)
DECLARE @Movil as varchar(20)
DECLARE @Email as varchar(60)
DECLARE @CLI_Telefono1 varchar(20)
DECLARE @CLI_Telefono2 varchar(20)
DECLARE @CLI_Telefono3 varchar(20)
DECLARE @CLI_Movil1 varchar(20)
DECLARE @CLI_Movil2 varchar(20)
DECLARE @CLI_Email1 varchar(60)
DECLARE @CLI_Email2 varchar(60)
DECLARE @CLI_Email3 varchar(60)
DECLARE Clientes_CURSOR CURSOR FOR
SELECT a.CLiID, a.EmpID, a.CliCtTel1, a.CliCtTel2, a.CliCTMov1, a.CliCtEma1,
       c.CliCtTel1, c.CliCtTel2, c.CliCtTel3, c.CliCtMov1, c.CliCtMov2, c.CliCtEma1, c.CliCtEma2, c.CliCtEma3
FROM AS_CliDir a INNER JOIN Clientes c ON a.CliID = c.CliID AND a.EmpID = c.EmpID WHERE (
    (a.CliCtTel1 IS NOT NULL AND a.CliCtTel1 NOT IN ('', c.CliCtTel1, c.CliCtTel2, c.CliCtTel3, c.CliCtMov1, c.CliCtMov2)) OR
    (a.CliCtTel2 IS NOT NULL AND a.CliCtTel2 NOT IN ('', c.CliCtTel1, c.CliCtTel2, c.CliCtTel3, c.CliCtMov1, c.CliCtMov2)) OR
    (a.CliCtMov1 IS NOT NULL AND a.CliCtMov1 NOT IN ('', c.CliCtTel1, c.CliCtTel2, c.CliCtTel3, c.CliCtMov1, c.CliCtMov2)) OR
    (a.CliCtEma1 IS NOT NULL AND a.CliCtEma1 NOT IN ('', c.CliCtEma1, c.CliCtEma2, c.CliCtEma3))
)

OPEN Clientes_CURSOR
FETCH NEXT FROM Clientes_CURSOR INTO
    @ClienteID, @CorreduriaID, @Telefono1, @Telefono2, @Movil, @Email,
    @CLI_Telefono1, @CLI_Telefono2, @CLI_Telefono3, @CLI_Movil1, @CLI_Movil2,
    @CLI_Email1, @CLI_Email2, @CLI_Email3
    WHILE @@fetch_status = 0
BEGIN
    IF @Telefono1 IS NOT NULL AND @Telefono1 NOT IN ('', @CLI_Telefono1, @CLI_Telefono2, @CLI_Telefono3, @CLI_Movil1, @CLI_Movil2)
    	IF @CLI_Telefono1 IS NULL OR @CLI_Telefono1 = ''
            UPDATE Clientes SET CliCtTel1 = @Telefono1 where EmpID = @CorreduriaID and CliID = @ClienteID
        ELSE
            IF @CLI_Telefono2 IS NULL OR @CLI_Telefono2 = ''
                UPDATE Clientes SET CliCtTel2 = @Telefono1 where EmpID = @CorreduriaID and CliID = @ClienteID
            ELSE
                IF @CLI_Telefono3 IS NULL OR @CLI_Telefono3 = ''
                    UPDATE Clientes SET CliCtTel3 = @Telefono1 where EmpID = @CorreduriaID and CliID = @ClienteID
                ELSE
                   PRINT 'Fallo espacio, telef:' + @Telefono1 + ' CliID:' + CAST(@ClienteID AS VARCHAR) + ' EmpID:' + CAST(@CorreduriaID AS VARCHAR)

	FETCH NEXT FROM Clientes_CURSOR INTO
    @ClienteID, @CorreduriaID, @Telefono1, @Telefono2, @Movil, @Email,
    @CLI_Telefono1, @CLI_Telefono2, @CLI_Telefono3, @CLI_Movil1, @CLI_Movil2,
    @CLI_Email1, @CLI_Email2, @CLI_Email3
END
CLOSE Clientes_CURSOR

OPEN Clientes_CURSOR
FETCH NEXT FROM Clientes_CURSOR INTO
@ClienteID, @CorreduriaID, @Telefono1, @Telefono2, @Movil, @Email,
@CLI_Telefono1, @CLI_Telefono2, @CLI_Telefono3, @CLI_Movil1, @CLI_Movil2,
@CLI_Email1, @CLI_Email2, @CLI_Email3
WHILE @@fetch_status = 0
BEGIN
    IF @Telefono2 IS NOT NULL AND @Telefono2 NOT IN ('', @CLI_Telefono1, @CLI_Telefono2, @CLI_Telefono3, @CLI_Movil1, @CLI_Movil2)
    	IF @CLI_Telefono1 IS NULL OR @CLI_Telefono1 = ''
            UPDATE Clientes SET CliCtTel1 = @Telefono2 where EmpID = @CorreduriaID and CliID = @ClienteID
        ELSE
            IF @CLI_Telefono2 IS NULL OR @CLI_Telefono2 = ''
                UPDATE Clientes SET CliCtTel2 = @Telefono2 where EmpID = @CorreduriaID and CliID = @ClienteID
            ELSE
                IF @CLI_Telefono3 IS NULL OR @CLI_Telefono3 = ''
                    UPDATE Clientes SET CliCtTel3 = @Telefono2 where EmpID = @CorreduriaID and CliID = @ClienteID
                ELSE
                   PRINT 'Fallo espacio, telef:' + @Telefono2 + ' CliID:' + CAST(@ClienteID AS VARCHAR) + ' EmpID:' + CAST(@CorreduriaID AS VARCHAR)

    IF @Movil IS NOT NULL AND @Movil NOT IN ('', @CLI_Telefono1, @CLI_Telefono2, @CLI_Telefono3, @CLI_Movil1, @CLI_Movil2)
        IF @CLI_Movil1 IS NULL OR @CLI_Movil1 = ''
            UPDATE Clientes SET CliCtMov1 = @Movil where EmpID = @CorreduriaID and CliID = @ClienteID
        ELSE
            IF @CLI_Movil2 IS NULL OR @CLI_Movil2 = ''
                UPDATE Clientes SET CliCtMov2 = @Movil where EmpID = @CorreduriaID and CliID = @ClienteID
            ELSE
               PRINT 'Fallo espacio, movil:' + @Movil + ' CliID:' + CAST(@ClienteID AS VARCHAR) + ' EmpID:' + CAST(@CorreduriaID AS VARCHAR)

    IF @Email IS NOT NULL AND @Email NOT IN ('', @CLI_Email1, @CLI_Email2, @CLI_Email3)
    	IF @CLI_Email1 IS NULL OR @CLI_Email1 = ''
            UPDATE Clientes SET CliCtEma1 = @Email where EmpID = @CorreduriaID and CliID = @ClienteID
        ELSE
            IF @CLI_Email2 IS NULL OR @CLI_Email2 = ''
                UPDATE Clientes SET CliCtEma2 = @Email where EmpID = @CorreduriaID and CliID = @ClienteID
            ELSE
                IF @CLI_Email3 IS NULL OR @CLI_Email3 = ''
                    UPDATE Clientes SET CliCtEma3 = @Email where EmpID = @CorreduriaID and CliID = @ClienteID
                ELSE
                   PRINT 'Fallo espacio, email:' + @Email + ' CliID:' + CAST(@ClienteID AS VARCHAR) + ' EmpID:' + CAST(@CorreduriaID AS VARCHAR)

FETCH NEXT FROM Clientes_CURSOR INTO
@ClienteID, @CorreduriaID, @Telefono1, @Telefono2, @Movil, @Email,
@CLI_Telefono1, @CLI_Telefono2, @CLI_Telefono3, @CLI_Movil1, @CLI_Movil2,
@CLI_Email1, @CLI_Email2, @CLI_Email3
END
CLOSE Clientes_CURSOR
DEALLOCATE Clientes_CURSOR