-1

I have a PL/SQL file that has a loop structure. The script is as follows.

SET SERVEROUTPUT ON
 declare 
  c_id employee.id%type;
  c_name employee.name%type;
  c_address employee.address%type;
  CURSOR c_employee is 
     SELECT id, name, address from employee;
 begin 
  open c_employee;
  LOOP
    FETCH c_employee into c_id, c_name, c_address;
    EXIT when c_employee%notfound;
    dbms_output.put_line(c_id||' '||c_name||' '||c_address);
  END LOOP;
  close c_employee;
  end;
  /

When I run this from SQLPlus I get only the details of the first row but not the rest. What am I doing wrong? How to get all the outputs for the loop.

Debdipta Halder
  • 497
  • 1
  • 5
  • 19

2 Answers2

-2

Try to convert your code to use a for loop instead of the open statement, like so -

for r_employee in c_employee
  LOOP
    dbms_output.put_line(r_employee.c_id||' '||r_employee.c_name||' '||r_employee.c_address);
  END LOOP;

Where r_employee is a variable of employee%type.

The way you currently wrote it does not iterate through the cursor, and this is why only the first row is presented.

Yaron Idan
  • 6,207
  • 5
  • 44
  • 66
-2

Even though your code looks correct, it should iterate through all the row not just one. Try to use below snippet and run it in SQL plus if still single row then there may be some other issue.

SET SERVEROUTPUT ON
DECLARE
BEGIN
  FOR I IN
  (SELECT id, name, address FROM employee
  )
  LOOP
    dbms_output.put_line(I.ID||' '||I.name||' '||I.address);
  END LOOP;
END;
/
Avrajit Roy
  • 3,233
  • 1
  • 13
  • 25