Monday, February 18, 2013

Select records from a cursor into PL/SQL table

declare
  -- Declare the PL/SQL table
  type deptarr is table of dept%rowtype
       index by binary_integer;
  d_arr deptarr;

  -- Declare cursor
  type d_cur is ref cursor return dept%rowtype;
  c1 d_cur;

  i number := 1;
begin
  -- Populate the PL/SQL table from the cursor
  open c1 for select * from dept;
  loop
    exit when c1%NOTFOUND;
    fetch c1 into d_arr(i);
    i := i+1;
  end loop;
  close c1;

  -- Display the entire PL/SQL table on screen
  for i in 1..d_arr.last loop
    dbms_output.put_line('DEPTNO : '||d_arr(i).deptno );
    dbms_output.put_line('DNAME  : '||d_arr(i).dname  );
    dbms_output.put_line('LOC    : '||d_arr(i).loc    );
    dbms_output.put_line('---------------------------');
  end loop;
end;

No comments:

Post a Comment