Apa perlunya loop di sini? Anda harus menulis ulang pernyataan pembaruan Anda menjadi seperti ini:
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));
Kemudian ulangi hal yang sama untuk 3, 2, dan 1 sebagai berikut (untuk 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));
PERBARUI:
Anda juga dapat mengulang 4 hingga 1 untuk menyelesaikan tugas
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;