Thursday, 1 December 2011

Fetch value from two more table and insert into another table using cursor in Oracle

1. Create table1 contains column with empid.

2. Create table2 contains column with firstname,lastname, empid(reference with table1 empid)

3. Create table3 contains column with empid,firstname,lastname

4. Add some data into table1 and table2.

5. Finally Run the below command:

DECLARE
CURSOR MyCursor IS
SELECT t1.empid,t2.firstname,t3.lastname
from table1 t1,table2 t2 WHERE t1.empid>100;
BEGIN
FOR MyCursors IN MyCursor LOOP
INSERT INTO table3 (empid,firstname,lastname)
VALUES (MyCursors.empid,MyCursors.firstname, Mycursors.lastname);
END LOOP;
COMMIT;
END;