I am new to PL/SQL . I have a requirement to update phone number in table employee_details where in the old phone number and new phone number is stored in another table phone_no (columns are old_phone_no
and new_phone_no
)
I want to take the first row in phone_no
(old_phone_no
and new_phone_no
) and execute update statement on employee_details i.e (update employee_details set phone_no=new_phone_no where phone_no=old_phone_no
) with periodic commits;
The same process should continue until we have traversed all the rows in phone_no table.
Looking for answers from all the experts on Stack Overflow.
Create statements.
--Table which needs to be updated
CREATE TABLE EMPLOYEE_DETAILS
(
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
PhoneNumber varchar(255)
);
-- Temp Table which contains old and new phone numbers
CREATE TABLE PHONE_NO
(
PersonID int,
OldPhone varchar(255),
NewPhone varchar(255)
);
I was thinking of periodic commits as I have some 10 million rows in EMPLOYEE_DETAILS
and we have many rows in EMPOYEE_DETAILS
for single phone number i.e OldPhone
from table PHONE_NO
.
I do not want to disturb the production performance. I am okay if there is any other way to do it.
Whether the below method works .
DECLARE CURSOR all_phones IS SELECT OldPhone, NewPhone FROM PHONE_NO ORDER by OldPhone;
TYPE phone_old IS TABLE OF PHONE_NO.OldPhone%TYPE;
TYPE phone_new IS TABLE OF PHONE_NO.NewPhone%TYPE;
phone_olds phone_old;
phone_news phone_new;
inx1 PLS_INTEGER;
BEGIN OPEN all_phones; FETCH all_phones BULK COLLECT INTO phone_olds, phone_news; CLOSE all_phones;
FOR inx1 IN 1..phone_olds.count LOOP
begin
loop
update EMPLOYEE_DETAILS
set PhoneNumber = phone_news(inx1)
where PhoneNumber = phone_olds(inx1)
and rowcount <= 10000;
continue when sql%notfound;
commit;
end loop
commit;
END LOOP;
END;
Regards, Jay.