Oracle
 sql >> Datenbank >  >> RDS >> Oracle

Identifizieren Sie Telefonnummern in einer PL/SQL-Iteration

Wozu braucht man hier eine Schleife? Sie müssen Ihre Update-Anweisung in etwa so umschreiben:

UPDATE t_numbers tn
   SET tn.country = (SELECT ic.country
                       FROM int_codes ic
                      WHERE ic.int_code = substr(tn.phone_number, 1, 4))
 WHERE tn.country is null
   AND EXISTS (SELECT 1
                 FROM int_codes ic
                WHERE ic.int_code = substr(tn.phone_number, 1, 4));

Wiederholen Sie dann dasselbe für 3, 2 und 1 wie folgt (für 3):

UPDATE t_numbers tn
   SET tn.country = (SELECT ic.country
                       FROM int_codes ic
                      WHERE ic.int_code = substr(tn.phone_number, 1, 3))
 WHERE tn.country is null
   AND EXISTS (SELECT 1
                 FROM int_codes ic
                WHERE ic.int_code = substr(tn.phone_number, 1, 3));

AKTUALISIERUNG:

Sie können auch 4 zu 1 durchlaufen, um die Aufgabe zu lösen

  begin
    for i in 1..4 loop
      UPDATE t_numbers tn
       SET tn.country = (SELECT ic.country
                           FROM int_codes ic
                          WHERE ic.int_code = substr(tn.phone_number, 1, (5-i)))
     WHERE tn.country is null
       AND EXISTS (SELECT 1
                     FROM int_codes ic
                    WHERE ic.int_code = substr(tn.phone_number, 1, (5-i)));
    end loop;
  END;