定义一个块,块中说明异常情况下如何处理就可以了。
begin
select 字段 into 变量...
exception
when NO_DATA_FOUND then --未找到时
处理逻辑
when exception_code then --这里的错误代码可以在standard包中找到
处理逻辑
when OTHERS then --default错误时
处理逻辑
end;
错误代码可通过如下语句获得:
select * from dba_source where owner='SYS' and name='STANDARD' and type='PACKAGE' and text like ' pragma EXCEPTION%';
最简单的语句:就是在你的字段加上max函数。如
select max(id)into var_id from tab where id=某一个值
呵呵,谁用谁明白。
exception
when no_data_found then
--你的处理逻辑
exception
when no_data_found then
null;