Solved

Cursor

Posted on 2007-11-26
2
1,649 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
2 Comments
 
LVL 23

Assisted Solution

by:Ashish Patel
Ashish Patel earned 100 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 150 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

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ā€¦
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now