v_rec temptab%rowtype;
cursor mycursor(cid number)
is
select * from temptab
where id<=cid;
begin
open mycursor(2);
while mycursor%isopen loop isopen 来判断游标是否打开
fetch mycursor into v_rec;
ifmycursor%notfound then notfound 判断游标中是否有数据
exit;
end if;
dbms_output.put_line(v_rec.id||' '||v_rec.name);
end loop;
close mycursor; 关闭游标
end;
异常:
异常类型名
too_many_rows 一个变量但却返回多条数据
others 所有其它异常
绑定异常的错误号
pragma exception_init(异常名,绑定的错误号); 绑定后出现此类异常时 错误号为绑定好的
raise 异常名 手动抛出异常
来看一个异常使用的例子
declare
v_rec temptab%rowtype;
abc exception; 自定义异常
pragmaexception_init(abc,-0001); 为异常绑定错误号
begin
select * into v_rec from temptabwhere;
if 2>1 then
raise abc; 手动抛出异常
end if;
insert into temptab valuesv_rec;
exception
when too_many_rows then 出现异常后再excepion标签后进行捕获
dbms_output.put_line('too_many_rowsexception');
when abc then
dbms_output.put_line('abcexception');
when others then
dbms_output.put_line('othersexception');
end;--others要放在最后