Tuesday, January 22, 2013

PL/SQL for loop example

Following PL/SQL script updates all phone number in user table using a simple for loop.
DECLARE
  phone_no varchar2(100) := null;
BEGIN
  FOR r1 IN (select user_name from tbl_user where phone is null) LOOP
    select phone into phone_no from tbl_profile where user_name = r1.user_name;
    update tbl_user set phone = phone_no where user_name = r1.user_name;
  END LOOP;
END;