PL/SQLブロックでhello worldとカーソル操作の初歩

set serveroutput on;

declare
  d date;
  rec jobs%rowtype;
  cursor c1 (param varchar2) is
    select * from jobs where job_id like '%' || param
  ;
begin
  dbms_output.put_line('hello world');

  -- 暗黙カーソルはSELECTとかDMLを実行するたびにオープンし、実行直後にクローズする。
  -- ただし属性値は保持され続ける。「SQL%ほにゃらら」という書式で参照できる。
  select sysdate into d from dual;
  if sql%found THEN
    dbms_output.put_line(sql%rowcount || ' selected');
  else
    dbms_output.put_line('failed to select');
  end if; -- => 1 selected

  -- 明示カーソルは、openしてぐるぐるfetchしてcloseするか、for loopでぐるぐるするかのどちらか。
  dbms_output.put_line('explicit cursor1');
  open c1('&1');
  loop
    fetch c1 into rec;
    exit when c1%notfound;
    dbms_output.put_line(rec.job_id || ', ' || rec.job_title);
  end loop;
  /* => PU_CLERK, Purchasing Clerk
        SH_CLERK, Shipping Clerk
        ST_CLERK, Stock Clerk
  */
  close c1;

  dbms_output.put_line('explicit cursor2');
  for rec2 in c1('&1')
  loop
    dbms_output.put_line(rec2.job_id || ', ' || rec2.job_title);
  end loop;
  /* => PU_CLERK, Purchasing Clerk
        SH_CLERK, Shipping Clerk
        ST_CLERK, Stock Clerk
  */

  -- 暗黙カーソルでもfor loopを書ける
  dbms_output.put_line('implicit cursor');
  for rec3 in (select * from jobs where job_id like '%&1')
  loop
    dbms_output.put_line(rec3.job_id || ', ' || rec3.job_title);
  end loop;

  rollback;
exception
  when others then
    rollback;
    raise;
end;
/