1.自定义异常的应用
sqlcode函数用来返回内部异常代码。
sqlerrm函数返回错误信息。
dbms_utility.format_error_backtrace 返回错误行号。
declare
error exception; --定义异常
result varchar2(2000);
begin
update zc set provider_name = provider_name || id where id = 529528;
if sql%found then --如果更新数据了,返回true;
dbms_output.put_line('true');
elsif sql%notfound then --如果未更新数据了,返回false;
result := 'ZC未更新数据!';
raise error;--抛出异常
end if;
if sql%rowcount = 0 then --sql%rowcount为变更数据的数目
dbms_output.put_line(sql%rowcount);
end if;
exception
when error then
dbms_output.put_line(result);
rollback;
when others then
dbms_output.put_line(sqlerrm);
dbms_output.put_line(dbms_utility.format_error_backtrace);
rollback;
end;
GOTO语句的应用:
declare
error exception; --定义异常
result varchar2(2000);
begin
update zc set provider_name = provider_name || id where id = 529528;
if sql%found then --如果更新数据了,返回true;
goto success1;
elsif sql%notfound then --如果未更新数据了,返回false;
goto error1;
end if;
if sql%rowcount = 0 then --sql%rowcount为变更数据的数目
goto error1;
end if;
<<success1>>
dbms_output.put_line('true');
<<error1>>
dbms_output.put_line('ZC未更新数据!');
exception
when others then
dbms_output.put_line(sqlerrm);
rollback;
end;