?
Solved

Cursor

Posted on 2007-11-26
2
Medium Priority
?
1,655 Views
Last Modified: 2013-12-07
Below code doesn't work

================================================
DECLARE
  empnum  tablename.empno%TYPE;
  name    tablename.ename%TYPE;
  TYPE cursor_var IS REF CURSOR;
  myCursorVar cursor_var;
BEGIN
  OPEN myCursorVar FOR SELECT empno,ename FROM tablename;
 
  for x IN myCursorVar LOOP
      DBMS_OUTPUT.PUT_LINE(x.empnum|| '  '|| x.name|| ' rowcount is ' || myCursorVar%ROWCOUNT);
 END LOOP;
 CLOSE myCursorVar;
=======================

but I change the
" for loop under BEGIN structure with below mention LOOP and END LOOP , it is working" --

LOOP
    FETCH myCursorVar INTO empnum,name;
    EXIT WHEN myCursorVar%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE(name||'  '||empnum||' rowcount is '||
      myCursorVar%ROWCOUNT);
  END LOOP;


Question :: why it is not working with FOR loop ? or I am doing any mistakes

0
Comment
Question by:gauravflame
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Assisted Solution

by:Ashish Patel
Ashish Patel earned 400 total points
ID: 20356211
In most situations that require an explicit cursor, you can simplify coding by using a cursor FOR loop instead of the OPEN, FETCH, and CLOSE statements. A cursor FOR loop implicitly declares its loop index as a record that represents a row fetched from the database. Next, it opens a cursor, repeatedly fetches rows of values from the result set into fields in the record, then closes the cursor when all rows have been processed. In the following example, the cursor FOR loop implicitly declares emp_rec as a record: See the example below.

DECLARE
   CURSOR c1 IS
      SELECT ename, sal, hiredate, deptno FROM emp;
   ...
BEGIN
   FOR emp_rec IN c1 LOOP
      ...
      salary_total :=  salary_total + emp_rec.sal;
   END LOOP;

Open in new window

0
 
LVL 27

Accepted Solution

by:
sujith80 earned 600 total points
ID: 20356338
>> why it is not working with FOR loop ? or I am doing any mistakes

Yes. REF CURSORs doesnt support the cursor FOR loop syntax.
You have to explicitly open-fetch-close ref-cursors.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question