代码改变世界

PLSQL 申明和游标

2016-12-09 17:20  甘雨路  阅读(442)  评论(0编辑  收藏  举报
--从键盘输入一个数
accept b prompt '请输入一个大于零的数字';
declare
 anum number := &b;
begin
  while anum>0
    loop
      dbms_output.put_line(anum);
      anum:=anum-1;
    end loop;
end;




declare
    v_num number;
begin
-- 从stsu表中选出id最大的值,并根据该值打印次数
  select max(id) into v_num from stsu;
  loop
  dbms_output.put_line(v_num);
  v_num := v_num-1;
  exit when v_num=0;
  end loop;
end;




declare
    cursor cur is select id,math from stsu;
begin
    for cur in (select id,math from stsu)
      loop
        dbms_output.put_line(cur.id ||'编号学员的数学分数:'||cur.math);
      end loop;
end;



declare 
  cursor cursor_id is select id,math from stsu;
  v_id stsu.id%type;
  v_math stsu.math%type;
begin 
  --打开游标
  open cursor_id;
  loop 
    -- 抓取数据
    fetch cursor_id into v_id,v_math;
    exit when cursor_id%notfound;
    dbms_output.put_line(v_id||'  '||v_math);
    end loop;
    -- 关闭游标
    close cursor_id;
end;