Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pl/sql cursor not dynamic?

Status
Not open for further replies.

wolves

Programmer
Jan 26, 2001
130
US
I am passing a value based on a value retrieve from another cursor. The cursor where the value is passed to always retrieves the same number of rows, basically it just returns the value from just one variable.
For example, cursor cur_prog always displays the first value returned from cursor cur_progs. I display cursor cur_progrs to make sure I am fetching all the rows and it is. Can I not change my variable C_MONTHS to change and fetch again?

Code:
cursor cur_progs is
SELECT DISTINCT FISCAL_MO
FROM   TABLE1
ORDER BY FISCAL_MO;

cursor cur_prog is
SELECT COUNT(*) 
FROM   TABLE2
WHERE  FISCAL_MONTHS = C_MONTHS;
  
 
------Declaring procedure names
PROCEDURE A_UPDATE_PROG;
--========================================================
-- PROCEDURE AND SQL STATEMENTS
--========================================================
PROCEDURE A_UPDATE_PROG IS

BEGIN
dbms_output.put_line('COUNTING PROGRAMS ');  

  C_CTR := 0;

OPEN cur_progS;
FETCH cur_progs INTO C_MONTHS;

OPEN cur_prog;

LOOP

FETCH cur_prog INTO C_CTR;
FETCH cur_progs INTO C_MONTHS;
  EXIT WHEN C_MONTHs='09';
dbms_output.put_line('Next Months:  ' ||C_MONTHS);

END LOOP;
 
You can fetch again, but until you reopen the cursor, the cur_prog cursor is not repopulated. Consequently, the cur_prog cursor will only be populated for the first row fetched from the cur_progs cursor.
Try this:
Code:
PROCEDURE A_UPDATE_PROG IS
BEGIN
   dbms_output.put_line('COUNTING PROGRAMS ');  
   FOR c_months IN (SELECT DISTINCT FISCAL_MO
                      FROM   TABLE1
                     ORDER BY FISCAL_MO) LOOP
      FOR j IN (SELECT COUNT(*) 
                  FROM   TABLE2
                 WHERE  FISCAL_MONTHS = c_months) LOOP
         dbms_output.put_line('Next Months: '||c_months); 
      END LOOP;
      EXIT WHEN c_months = '09';
   END LOOP;
END;
 
Thanks very much for the tip carp.
works like a charm.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top