Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i retrieve two or more tables using cursor?

DECLARE
	date1 DATE := '&first_date'; 
	date2 DATE := '&second_date'; 
	CURSOR emp_cursor (date1 DATE, date2 DATE) IS
	SELECT first_name,
		last_name,
		hire_date
	FROM employees
	WHERE hire_date BETWEEN date1 AND date2;
		TYPE fname IS TABLE OF employees.first_name%TYPE INDEX BY PLS_INTEGER;
		TYPE lname IS TABLE OF employees.last_name%TYPE INDEX BY PLS_INTEGER;
		TYPE hdate IS TABLE OF employees.hire_date%TYPE INDEX BY PLS_INTEGER;
		f fname;
		l lname;
		h hdate;
BEGIN
	OPEN emp_cursor(date1,date2);
		LOOP
	FETCH emp_cursor BULK COLLECT INTO f, l, h;
		FOR ctr IN 1 .. f.COUNT()
		LOOP
			DBMS_OUTPUT.PUT_LINE('Name: '|| f(ctr) || ' ' || l(ctr));
			DBMS_OUTPUT.PUT_LINE('Date Hired: '|| h(ctr));
			DBMS_OUTPUT.PUT_LINE('------------------------------------');
		END LOOP;
			DBMS_OUTPUT.PUT_LINE(emp_cursor%ROWCOUNT || ' number of rows retrieved');
		EXIT WHEN emp_cursor%NOTFOUND;
		END LOOP;
	CLOSE emp_cursor;
END;
Posted
Updated 12-Sep-10 1:15am
v2

1 solution

Can you use JOIN on those tables?

If they have a similar structures you could do a UNION and get all records.
 
Share this answer
 
Comments
dianenacario 12-Sep-10 7:22am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900