ここでループの必要性は何ですか?更新ステートメントを次のように書き直す必要があります:
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));
次に、次のように3、2、および1について同じことを繰り返します(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));
更新:
4対1でループして、タスクを実行することもできます
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;