How do I access the %ROWCOUNT for an implicit cursor in a cursor FOR loop, as in (select * from dept) loop?
Answer:
Raymond, if you want to get information about the status of your cursor inside a cursor FOR loop, you should NOT use an implicit cursor within the loop statement, as you suggest in your question. In other words, if I worked with a table defined as follows:
CREATE TABLE question (
title VARCHAR2(100),
description VARCHAR2(2000));
INSERT INTO question VALUES ('Use implicit?',
'How can I access SQL% cursor attributes inside a cursor FOR loop
with an implicit cursor');
INSERT INTO question VALUES ('Use explicit?',
'How can I access cursor attributes inside a cursor FOR loop
with an explicit cursor?');
then I would write code like this:
DECLARE
CURSOR question_cur
IS
SELECT *
FROM question;
BEGIN
FOR rec IN question_cur
LOOP
DBMS_OUTPUT.put_line (question_cur%ROWCOUNT);
END LOOP;
END;
/
You can, in fact, reference SQL%ROWCOUNT inside a cursor FOR loop built around an implicit cursor, but it turns out that the SQL% cursor attributes are not changed by the actions of a implicit-based cursor FOR loop. Check out the rowcount.sql script to see a demonstration of this behavior.
And if you don't really need to obtain attribute information for each row fetched, you are even better off using a BULK COLLECT, as in:
DECLARE
TYPE question_tc IS TABLE OF question%ROWTYPE
INDEX BY PLS_INTEGER;
l_questions question_tc;
BEGIN
SELECT *
BULK COLLECT INTO l_questions FROM question;
DBMS_OUTPUT.put_line (SQL%ROWCOUNT);
DBMS_OUTPUT.put_line (l_questions.COUNT);
END;
nice example.....
ReplyDelete