cursors in pl/sql includes cursor example and continuation of first cursor example please use...
TRANSCRIPT
Cursors in PL/SQLIncludes cursor example and
continuation of first cursor example
Please use speaker notes for additional information!
Explicit cursorExplicit cursor
When using an explicit cursor in PL/SQL, there are four things that must be accomplished by the programmer:
• The cursor must be declared
• The cursor needs to be opened
• Fetch the results of the query into the variables declared in PL/SQL
• The cursor needs to be closed
DECLARE v_name donor.name%TYPE; v_yrgoal donor.yrgoal%TYPE; v_state donor.state%TYPE; CURSOR donor_cursor IS SELECT name, yrgoal, state FROM donor;BEGINOPEN donor_cursor;FETCH donor_cursor INTO v_name, v_yrgoal, v_state;WHILE donor_cursor%FOUND LOOP INSERT INTO donor_part VALUES(v_name, v_yrgoal, v_state); FETCH donor_cursor INTO v_name, v_yrgoal, v_state;END LOOP;CLOSE donor_cursor;END;/
Explicit cursorsExplicit cursors
SQL> edit cursor1
SQL> @ cursor1
PL/SQL procedure successfully completed.
SQL> SELECT * FROM donor_part;
NAME YRGOAL ST--------------- --------- --Stephen Daniels 500 MAJennifer Ames 400 RICarl Hersey RISusan Ash 100 MANancy Taylor 50 MARobert Brooks 50 MA
The table donor_part was empty before cursor1 was executed. After running the anonymous block, there are now six records in the table. They correspond to the six records that were in the donor table.
Explicit cursorExplicit cursor
SQL> SELECT * FROM donor;
IDNO NAME STADR CITY ST ZIP DATEFST YRGOAL CONTACT----- --------------- --------------- ---------- -- ----- --------- --------- ------------11111 Stephen Daniels 123 Elm St Seekonk MA 02345 03-JUL-98 500 John Smith12121 Jennifer Ames 24 Benefit St Providence RI 02045 24-MAY-97 400 Susan Jones22222 Carl Hersey 24 Benefit St Providence RI 02045 03-JAN-98 Susan Jones23456 Susan Ash 21 Main St Fall River MA 02720 04-MAR-92 100 Amy Costa33333 Nancy Taylor 26 Oak St Fall River MA 02720 04-MAR-92 50 John Adams34567 Robert Brooks 36 Pine St Fall River MA 02720 04-APR-98 50 Amy Costa
6 rows selected.
SQL> SELECT * FROM donor_part;
NAME YRGOAL ST--------------- --------- --Stephen Daniels 500 MAJennifer Ames 400 RICarl Hersey RISusan Ash 100 MANancy Taylor 50 MARobert Brooks 50 MA
Initial FETCH got the first record from the table and put the data into the variables. The INSERT inside the loop put the data from the variables into the new table.
The FETCH after the INSERT (the last command in the loop) got the second record from the table and put the data in the variables. The INSERT inside the loop put the data from the variables into the new table.
The FETCH after the INSERT (the last command in the loop) got the third record from the table and put the data in the variables. The INSERT inside the loop put the data from the variables into the new table.
DECLARE v_name donor.name%TYPE; v_yrgoal donor.yrgoal%TYPE; v_state donor.state%TYPE; CURSOR donor_cursor IS SELECT name, yrgoal, state FROM donor;BEGIN OPEN donor_cursor; FETCH donor_cursor INTO v_name, v_yrgoal, v_state; WHILE donor_cursor%FOUND LOOP INSERT INTO donor_part VALUES(v_name, v_yrgoal, v_state); FETCH donor_cursor INTO v_name, v_yrgoal, v_state; END LOOP; CLOSE donor_cursor;END;/
Explicit cursorExplicit cursorThese are the variable names declared to receive the data from the table.
The cursor is created with a select statement. The select statement will be processed by the cursor providing the rows to be processed in the block.
The OPEN statement opens or activates the cursor - this means the select is executed to fill the cursor with rows.
The FETCH statement gets the first record in the cursor and moves the data to the defined variables. This is the initial FETCH.
The FETCH which is the last statement in the loop will get all other records.The WHILE loop will continue to execute while there is still
data in the cursor. This is tested with the %FOUND. Note that when the loop is entered, the FETCH of the initial record has already been done. The INSERT statement will insert the data from that record into the table named donor_part. Then it will execute the FETCH which is the last statement in the loop to get the next record. As long as a record is found, the INSERT will be done followed by another FETCH. When the FETCH is unsuccessful, the WHILE will terminate because of donor_cursor%FOUND.
When the loop is complete the cursor is closed.
INSERT puts a record into donor_part containing the information that the FETCH put into the variables.
DECLARE v_name donor.name%TYPE; v_yrgoal donor.yrgoal%TYPE; v_state donor.state%TYPE; CURSOR donor_cursor IS SELECT name, yrgoal, state FROM donor;BEGINOPEN donor_cursor;FETCH donor_cursor INTO v_name, v_yrgoal, v_state;WHILE donor_cursor%FOUND LOOP IF v_yrgoal > 50 THEN INSERT INTO donor_part VALUES(v_name, v_yrgoal, v_state); END IF; FETCH donor_cursor INTO v_name, v_yrgoal, v_state;END LOOP;CLOSE donor_cursor;END;/
Explicit cursorExplicit cursor
SQL> edit cursor2
SQL> @ cursor2
PL/SQL procedure successfully completed.
SQL> SELECT * FROM donor_part;
NAME YRGOAL ST--------------- --------- --Stephen Daniels 500 MAJennifer Ames 400 RISusan Ash 100 MA
The IF statement only INSERTs records where the year goal is greater than 50. Only the three records shown met the criteria.
DECLARE v_name donor.name%TYPE; v_yrgoal donor.yrgoal%TYPE; v_state donor.state%TYPE; CURSOR donor_cursor IS SELECT name, yrgoal, state FROM donor WHERE yrgoal> 50;BEGINOPEN donor_cursor;FETCH donor_cursor INTO v_name, v_yrgoal, v_state;WHILE donor_cursor%FOUND LOOPINSERT INTO donor_part VALUES(v_name, v_yrgoal, v_state);FETCH donor_cursor INTO v_name, v_yrgoal, v_state;END LOOP;CLOSE donor_cursor;END;/
Explicit cursorExplicit cursor
SQL> edit cursor2a
SQL> @ cursor2a
PL/SQL procedure successfully completed.
SQL> SELECT * FROM donor_part;
NAME YRGOAL ST--------------- --------- --Stephen Daniels 500 MAJennifer Ames 400 RISusan Ash 100 MA
Instead of selecting the record after they have been FETCHed with the IF, you can SELECT the records that meet the condition in the CURSOR with the WHERE clause.
Explicit cursorExplicit cursorDECLARE v_name donor.name%TYPE; v_yrgoal donor.yrgoal%TYPE; v_state donor.state%TYPE; CURSOR donor_cursor IS SELECT name, yrgoal, state FROM donor WHERE yrgoal> 50;BEGINOPEN donor_cursor;FETCH donor_cursor INTO v_name, v_yrgoal, v_state;LOOP INSERT INTO donor_part VALUES(v_name, v_yrgoal, v_state); FETCH donor_cursor INTO v_name, v_yrgoal, v_state; EXIT WHEN donor_cursor%NOTFOUND;END LOOP;CLOSE donor_cursor;END;/
SQL> edit cursor2b
SQL> @ cursor2b
PL/SQL procedure successfully completed.
SQL> SELECT * FROM donor_part;
NAME YRGOAL ST--------------- --------- --Stephen Daniels 500 MAJennifer Ames 400 RISusan Ash 100 MA
This code changes to a simple LOOP with an exit based on %NOTFOUND instead of %FOUND.
DECLARE v_name donor.name%TYPE; v_yrgoal donor.yrgoal%TYPE; v_state donor.state%TYPE; CURSOR donor_cursor IS SELECT name, yrgoal, state FROM donor;BEGINOPEN donor_cursor;FETCH donor_cursor INTO v_name, v_yrgoal, v_state;WHILE donor_cursor%ROWCOUNT < 5 AND donor_cursor%FOUND LOOP INSERT INTO donor_part VALUES(v_name, v_yrgoal, v_state); FETCH donor_cursor INTO v_name, v_yrgoal, v_state;END LOOP;CLOSE donor_cursor;END;/
Explicit cursor
Explicit cursor
SQL> @ cursor3
PL/SQL procedure successfully completed.
SQL> SELECT * FROM donor_part;
NAME YRGOAL ST--------------- --------- --Stephen Daniels 500 MAJennifer Ames 400 RICarl Hersey RISusan Ash 100 MA
The while loop will terminate after 4 records have been processed or when no more records are in the cursor. %ROWCOUNT is used to determine when 4 records have been processed.
WHILE donor_cursor%ROWCOUNT < 5 AND donor_cursor%FOUND LOOP
LogicLogic
Rowcount > 4
Stop processing
YN
No records
Stop processing
YN
Process
OR LOGIC: Logic for if row count is > 4 OR there are no more records stop processing. Otherwise process the records.
Rowcount < 5YN
Records to process
Process
YN
Stopprocessing
Stopprocessing
AND LOGIC: Logic for if row count is < 5 and there are records to process, process. If either condition is false, do not process.
SET SERVEROUTPUT ONDECLARE v_drive_no drive.driveno%TYPE; v_drive_name drive.drivename%TYPE; v_contamt donation.contamt%TYPE; v_tot_contamt cont_info.contamt%TYPE; CURSOR drive_cursor IS SELECT driveno, drivename FROM drive ORDER BY driveno; CURSOR donation_cursor IS SELECT contamt FROM donation WHERE v_drive_no = driveno ORDER BY driveno; BEGIN OPEN drive_cursor; LOOP FETCH drive_cursor INTO v_drive_no, v_drive_name; EXIT WHEN drive_cursor%NOTFOUND; IF donation_cursor%ISOPEN THEN CLOSE donation_cursor; END IF; OPEN donation_cursor; v_tot_contamt := 0; LOOP FETCH donation_cursor INTO v_contamt; EXIT WHEN donation_cursor%NOTFOUND; v_tot_contamt := v_tot_contamt + v_contamt; dbms_output.put_line('The current amount is: '||v_tot_contamt); END LOOP; INSERT into cont_info VALUES(v_drive_no, v_drive_name, v_tot_contamt); CLOSE donation_cursor; END LOOP; CLOSE drive_cursor;END;/SET SERVEROUTPUT OFF
Explicit cursorExplicit cursor
Inner loop that will process the information in the donation cursor.
Outer loop that processes the information in the drive table.
SQL> edit cursor6a
SQL> SELECT * FROM drive;
DRI DRIVENAME DRIVECHAIR LASTYEAR THISYEAR--- --------------- ------------ --------- ---------100 Kids Shelter Ann Smith 10000 0200 Animal Home Linda Grant 5000 0300 Health Aid David Ross 7000 0400 Half Way Robert Doe 0 0
Data and resultsData and results
SQL> SELECT * FROM cont_info;
DRI DRIVENAME CONTAMT--- --------------- ---------100 Kids Shelter 105200 Animal Home 75300 Health Aid 20400 Half Way 0
SQL> @ cursor6a
The current amount is: 25The current amount is: 45The current amount is: 55The current amount is: 105The current amount is: 40The current amount is: 75The current amount is: 10The current amount is: 20
PL/SQL procedure successfully completed.
SQL> SELECT * FROM donation 2 ORDER by driveno;
IDNO DRI CONTDATE CONTAMT----- --- --------- ---------11111 100 07-JAN-99 2523456 100 03-MAR-99 2022222 100 14-MAR-99 1012121 100 04-JUN-99 5012121 200 23-FEB-99 4011111 200 12-JUN-99 3533333 300 10-MAR-99 1023456 300 14-JUN-99 10
drive_cursordrive_cursor
100 Kids Shelter200 Animal Home300 Health Aid400 Half Way
CURSOR drive_cursor IS SELECT driveno, drivename FROM drive ORDER BY driveno;
Explicit cursorExplicit cursor
CURSOR donation_cursor IS SELECT contamt FROM donation WHERE v_drive_no = driveno ORDER BY driveno;
donation_cursordonation_cursor 25 20 10 50
FETCH drive_cursor INTO v_drive_no, v_drive_name;v_drive_no is now 100v_drive_name is now Kids Shelter
Note that v_drive_no is 100 so only records from donation where driveno = 100 are selected.
LOOP FETCH donation_cursor INTO v_contamt; EXIT WHEN donation_cursor%NOTFOUND; v_tot_contamt := v_tot_contamt + v_contamt; END LOOP;
v_contamt v_contamt v_tot_contamtv_tot_contamt 25 25 20 45 10 55 50 105
SQL> SELECT * FROM cont_info;
DRI DRIVENAME CONTAMT--- --------------- ---------100 Kids Shelter 105
INSERT into cont_info VALUES(v_drive_no, v_drive_name, v_tot_contamt);CLOSE donation_cursor;
At this point, the donation_cursor is closed and control returns to the outer loop where a FETCH is done from the drive_cursor getting 200 Animal Home. The inner loop is then entered and the donation cursor is filled with donations for drive number 200.
drive_cursordrive_cursor
100 Kids Shelter200 Animal Home300 Health Aid400 Half Way
CURSOR drive_cursor IS SELECT driveno, drivename FROM drive ORDER BY driveno;
Explicit cursorExplicit cursor
CURSOR donation_cursor IS SELECT contamt FROM donation WHERE v_drive_no = driveno ORDER BY driveno;
donation_cursordonation_cursor
40 35
FETCH drive_cursor INTO v_drive_no, v_drive_name;v_drive_no is now 200v_drive_name is now Animal Home
Note that v_drive_no is 200 so only records from donation where driveno = 200 are selected.
LOOP FETCH donation_cursor INTO v_contamt; EXIT WHEN donation_cursor%NOTFOUND; v_tot_contamt := v_tot_contamt + v_contamt; END LOOP;
v_contamt v_contamt v_tot_contamtv_tot_contamt 40 40 35 75
SQL> SELECT * FROM cont_info;
DRI DRIVENAME CONTAMT--- --------------- ---------100 Kids Shelter 105200 Animal Home 75
INSERT into cont_info VALUES(v_drive_no, v_drive_name, v_tot_contamt);CLOSE donation_cursor;
At this point, the donation_cursor is closed and control returns to the outer loop where a FETCH is done from the drive_cursor getting 300 Health Aid. The inner loop is then entered and the donation cursor is filled with donations for drive number 300.
drive_cursordrive_cursor
100 Kids Shelter200 Animal Home300 Health Aid400 Half Way
CURSOR drive_cursor IS SELECT driveno, drivename FROM drive ORDER BY driveno;
Explicit cursorExplicit cursor
CURSOR donation_cursor IS SELECT contamt FROM donation WHERE v_drive_no = driveno ORDER BY driveno;
donation_cursordonation_cursor
10 10
FETCH drive_cursor INTO v_drive_no, v_drive_name;v_drive_no is now 300v_drive_name is now Health Aid
Note that v_drive_no is 300 so only records from donation where driveno = 300 are selected.
LOOP FETCH donation_cursor INTO v_contamt; EXIT WHEN donation_cursor%NOTFOUND; v_tot_contamt := v_tot_contamt + v_contamt; END LOOP;
v_contamt v_contamt v_tot_contamtv_tot_contamt 10 10 10 20
SQL> SELECT * FROM cont_info;
DRI DRIVENAME CONTAMT--- --------------- ---------100 Kids Shelter 105200 Animal Home 75300 Health Aid 20
INSERT into cont_info VALUES(v_drive_no, v_drive_name, v_tot_contamt);CLOSE donation_cursor;
At this point, the donation_cursor is closed and control returns to the outer loop where a FETCH is done from the drive_cursor getting 400 Half Way. The inner loop is then entered and the donation cursor is filled with donations for drive number 400.
drive_cursordrive_cursor
100 Kids Shelter200 Animal Home300 Health Aid400 Half Way
CURSOR drive_cursor IS SELECT driveno, drivename FROM drive ORDER BY driveno;
Explicit cursorExplicit cursor
CURSOR donation_cursor IS SELECT contamt FROM donation WHERE v_drive_no = driveno ORDER BY driveno;
donation_cursordonation_cursor
FETCH drive_cursor INTO v_drive_no, v_drive_name;v_drive_no is now 400v_drive_name is now Half Way
Note that v_drive_no is 300 so only records from donation where driveno = 300 are selected.
LOOP FETCH donation_cursor INTO v_contamt; EXIT WHEN donation_cursor%NOTFOUND; v_tot_contamt := v_tot_contamt + v_contamt; END LOOP;
v_contamt v_contamt v_tot_contamtv_tot_contamt 0 0
SQL> SELECT * FROM cont_info;
DRI DRIVENAME CONTAMT--- --------------- ---------100 Kids Shelter 105200 Animal Home 75300 Health Aid 20400 Half Way 0
INSERT into cont_info VALUES(v_drive_no, v_drive_name, v_tot_contamt);CLOSE donation_cursor;
At this point, the donation_cursor is closed and control returns to the outer loop where a FETCH is done from the drive_cursor getting no data. Therefore the inner loop is exited and the drive_cursor is closed.
No donations to drive 400
SET SERVEROUTPUT ONDECLARE v_current_drive_no drive.driveno%TYPE; v_drive_name drive.drivename%TYPE; v_contamt donation.contamt%TYPE; v_tot_contamt cont_info.contamt%TYPE; CURSOR drive_cursor IS SELECT driveno, drivename FROM drive ORDER BY driveno; CURSOR donation_cursor(v_drive_no VARCHAR2) IS SELECT contamt FROM donation WHERE v_drive_no = driveno ORDER BY driveno; BEGIN OPEN drive_cursor; LOOP FETCH drive_cursor INTO v_current_drive_no, v_drive_name; EXIT WHEN drive_cursor%NOTFOUND; IF donation_cursor%ISOPEN THEN CLOSE donation_cursor; END IF; v_tot_contamt := 0; OPEN donation_cursor (v_current_drive_no); LOOP FETCH donation_cursor INTO v_contamt; EXIT WHEN donation_cursor%NOTFOUND; v_tot_contamt := v_tot_contamt + v_contamt; dbms_output.put_line('The current amount is: '||v_tot_contamt); END LOOP; INSERT into cont_info VALUES(v_current_drive_no, v_drive_name, v_tot_contamt); CLOSE donation_cursor; END LOOP; CLOSE drive_cursor;END;/SET SERVEROUTPUT OFF
Explicit cursorExplicit cursor
SQL> edit cursor7a2x
In this example the drive_cursor is opened and the first fetch puts the driveno from the first record into v_current_drive_no.
Then, when the donation cursor is opened it takes the drive no that is passed to it and opens the donation cursor looking for a match.
You could in reality pass any thing you want to the donation_cursor and have the WHERE clause tied to the pass.
Explicit cursorExplicit cursor SET SERVEROUTPUT ONDECLARE v_current_drive_no drive.driveno%TYPE; v_drive_name drive.drivename%TYPE; v_contamt donation.contamt%TYPE; v_tot_contamt cont_info.contamt%TYPE; CURSOR drive_cursor IS SELECT driveno, drivename FROM drive ORDER BY driveno; CURSOR donation_cursor(v_drive_no NUMBER) IS SELECT contamt FROM donation WHERE TO_CHAR(v_drive_no) = driveno ORDER BY driveno; BEGIN OPEN drive_cursor; LOOP FETCH drive_cursor INTO v_current_drive_no, v_drive_name; EXIT WHEN drive_cursor%NOTFOUND; IF donation_cursor%ISOPEN THEN CLOSE donation_cursor; END IF; v_tot_contamt := 0; OPEN donation_cursor (TO_NUMBER(v_current_drive_no)); LOOP FETCH donation_cursor INTO v_contamt; EXIT WHEN donation_cursor%NOTFOUND; v_tot_contamt := v_tot_contamt + v_contamt; dbms_output.put_line('The current amount is: '||v_tot_contamt); END LOOP; INSERT into cont_info VALUES(v_current_drive_no, v_drive_name, v_tot_contamt); CLOSE donation_cursor; END LOOP; CLOSE drive_cursor;END;/SET SERVEROUTPUT OFF
SQL> edit cursor7a2
This example differs from the previous one because it defines the drive number as a numeric and has to deal with appropriate conversion to make the processing work.
SQL> SELECT * FROM cont_info;
DRI DRIVENAME CONTAMT--- --------------- ---------100 Kids Shelter 105200 Animal Home 75300 Health Aid 20400 Half Way 0
SET SERVEROUTPUT ONDECLARE v_current_drive_no drive.driveno%TYPE; v_drive_name drive.drivename%TYPE; v_lastyear drive.lastyear%TYPE; v_calc drive.lastyear%TYPE; v_contamt donation.contamt%TYPE; v_tot_contamt cont_info.contamt%TYPE; CURSOR drive_cursor IS SELECT driveno, drivename, lastyear FROM drive ORDER BY driveno; CURSOR donation_cursor(v_drive_no VARCHAR2, v_calc NUMBER) IS SELECT contamt FROM donation WHERE v_drive_no = driveno and contamt > v_calc ORDER BY driveno; BEGIN OPEN drive_cursor; LOOP FETCH drive_cursor INTO v_current_drive_no, v_drive_name, v_lastyear; EXIT WHEN drive_cursor%NOTFOUND; IF donation_cursor%ISOPEN THEN CLOSE donation_cursor; END IF; v_calc := v_lastyear/500; v_tot_contamt := 0; OPEN donation_cursor (v_current_drive_no, v_calc); LOOP FETCH donation_cursor INTO v_contamt; EXIT WHEN donation_cursor%NOTFOUND; v_tot_contamt := v_tot_contamt + v_contamt; dbms_output.put_line('The current amount is: '||v_tot_contamt); END LOOP; INSERT into cont_info VALUES(v_current_drive_no, v_drive_name, v_tot_contamt); CLOSE donation_cursor; END LOOP; CLOSE drive_cursor;
END;/SET SERVEROUTPUT OFF
Explicit cursorExplicit cursor
SQL> edit cursor9
Explicit cursorExplicit cursor SQL> SELECT * FROM drive;
DRI DRIVENAME DRIVECHAIR LASTYEAR THISYEAR--- --------------- ------------ --------- ---------100 Kids Shelter Ann Smith 10000 0200 Animal Home Linda Grant 5000 0300 Health Aid David Ross 7000 0400 Half Way Robert Doe 0 0
The first row from the drive brings in 100 Kids Shelter 10000.
v_calc is 10000/500 or 20
The drive number of 100 and the calculation of 20 are passed to the donation cursor when the cursor is opened.
CURSOR donation_cursor(v_drive_no VARCHAR2, v_calc NUMBER) IS SELECT contamt FROM donation WHERE v_drive_no = driveno and contamt > v_calc ORDER BY driveno;
OPEN donation_cursor (v_current_drive_no, v_calc);
This means the first four records match drive number 100, but only the first and the fourth have contamt > 20.When 200 and 5000 are passed, the calculation results in 10 and both for 200 meet the 10 criteria.When 300 and 7000 are passed, the calculation results in 14 and while both records meet the 300, neither has a contamt greater than 14.
SQL> SELECT * FROM donation 2 ORDER by driveno;
IDNO DRI CONTDATE CONTAMT----- --- --------- ---------11111 100 07-JAN-99 2523456 100 03-MAR-99 2022222 100 14-MAR-99 1012121 100 04-JUN-99 5012121 200 23-FEB-99 4011111 200 12-JUN-99 3533333 300 10-MAR-99 1023456 300 14-JUN-99 10
SQL> @ cursor9The current amount is: 25The current amount is: 75The current amount is: 40The current amount is: 75
PL/SQL procedure successfully completed.SQL> SELECT * FROM cont_info;
DRI DRIVENAME CONTAMT--- --------------- ---------100 Kids Shelter 75200 Animal Home 75300 Health Aid 0400 Half Way 0
Explicit cursorExplicit cursor SQL> SELECT * FROM donation 2 ORDER by driveno;
IDNO DRI CONTDATE CONTAMT----- --- --------- ---------11111 100 07-JAN-99 2523456 100 03-MAR-99 2022222 100 14-MAR-99 1012121 100 04-JUN-99 5012121 200 23-FEB-99 4011111 200 12-JUN-99 3533333 300 10-MAR-99 1023456 300 14-JUN-99 10
SQL> SELECT * FROM drive;
DRI DRIVENAME DRIVECHAIR LASTYEAR THISYEAR--- --------------- ------------ --------- ---------100 Kids Shelter Ann Smith 10000 0200 Animal Home Linda Grant 5000 0300 Health Aid David Ross 7000 0400 Half Way Robert Doe 0 0
Explicit cursorExplicit cursor SET SERVEROUTPUT ONDECLARE v_drive_no drive.driveno%TYPE; v_drive_name drive.drivename%TYPE; v_contamt donation.contamt%TYPE; v_tot_contamt cont_info.contamt%TYPE; CURSOR drive_cursor IS SELECT driveno, drivename FROM drive ORDER BY driveno; CURSOR donation_cursor IS SELECT contamt FROM donation WHERE v_drive_no = driveno ORDER BY driveno; BEGIN OPEN drive_cursor; FETCH drive_cursor INTO v_drive_no, v_drive_name; WHILE drive_cursor%FOUND LOOP IF donation_cursor%ISOPEN THEN CLOSE donation_cursor; END IF; OPEN donation_cursor; v_tot_contamt := 0; FETCH donation_cursor INTO v_contamt; WHILE donation_cursor%FOUND LOOP v_tot_contamt := v_tot_contamt + v_contamt; dbms_output.put_line('The current amount is: '||v_tot_contamt); FETCH donation_cursor INTO v_contamt; END LOOP; INSERT into cont_info VALUES(v_drive_no, v_drive_name, v_tot_contamt); CLOSE donation_cursor; FETCH drive_cursor INTO v_drive_no, v_drive_name; END LOOP; CLOSE drive_cursor;END;/SET SERVEROUTPUT OFF
This shows the initializing fetch and the embedded fetch for the outer WHILE loop.
This shows the initializing fetch and the embedded fetch for the inner WHILE loop.
SQL> edit cursor7b