Hvad er behovet for en løkke her? Du skal omskrive din opdateringserklæring til noget som dette:
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));
Gentag derefter det samme for 3, 2 og 1 som følger (for 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));
OPDATERING:
Du kan også gå gennem 4 til 1 for at opnå opgaven
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;