Monday, December 11, 2017

ORACLE - How make cursor and how go through its rows

With Oracle cursor you can read table data by SQL.
is
  /* person`s list */

  cursor c_persons is
    select a.id, a.email
    from factory.an_users a
    where
    nvl( a.active, 'N' ) = 'A'
    ;

  pc_persons c_persons%rowtype;

  sEmail varchar2( 4000 );
begin
  ...
  sEmail := '';
  
  /* open cursor */ 
  open c_persons;
  loop

    /* read every rows of the cursor */
    fetch c_persons into pc_persons;
    exit when c_persons%notfound;

    /* -- action with row data.. */
    if pc_persons.email is not null then
      sEmail := sEmail || pc_persons.email || ';';
    end if;
    
  end loop;
  close c_persons;
  ...
end;

No comments:

Post a Comment