Solved

Cursor

Posted on 2007-11-26
2
1,652 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.

Question has a verified solution.

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

Suggested Solutions

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

791 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