René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Declaring records and cursors [PL/SQL]

create table city (
  id          number primary key,
  name        varchar2(50),
  population  number(8)
);

Cursors

Ref cursorss

declare
  type cur_type_name_1 is ref cursor;

  type cur_type_name_2 is ref cursor return city%rowtype;
  type cur_type_name_3 is ref cursor return cursor-name%rowtype;
  type cur_type_name_4 is ref cursor return cursor-variable-name%rowtype;

  type cur_type_name_5 is ref cursor return record-name%type;
  type cur_type_name_6 is ref cursor return record-type-name%type;
  type cur_type_name_7 is ref cursor return ref-cursor-type-name%type;
begin
  null;
end;
/

Difference beetween a cursor type and a cursor

create or replace package ref_cursor_example as
  type   rec_t is record (a number, b varchar2(10));

  type   ref_cur_t is ref cursor return rec_t;

  procedure do (c in ref_cur_t);
end ref_cursor_example;
/

show errors

create or replace package body ref_cursor_example as

  procedure do(c in ref_cur_t) is
    r  rec_t;
  begin

    fetch c into r;
    loop
      exit when c%notfound;

      fetch c into r;
    end loop;

  end do;

end ref_cursor_example;
/

Records

Using %rectype

declare
  city_rec city%rowtype;
begin
  null;
end;
/

Using type ... is record

declare
  type foo_rec is record (bar number(5), baz varchar2(20));
begin
  null;
end;
/

From a cursor

declare
  cursor cur_city is select name, population from city;
  rec_city cur_city%rowtype;
begin
  null;
end;
/

Nesting records

Records can be nested:
declare
  type foo_rec    is record (bar number(5), baz varchar2(20));
  type nested_rec is record (quux date, fr foo_rec);
begin
  null;
end;
/