测试SQL过程异常检查

时间:2015-08-31 18:10:55

标签: sql oracle exception-handling oracle-sqldeveloper procedure

我在Oracle SQL Developer 11g上创建了下面的PL / SQL过程,并且想要强制测试异常以确保我发送到它的电子邮件实际上在异常发生时发送出去。有人可以让我知道我该怎么做吗?

create or replace
procedure Procedure_1 as
-- This script updates the 'owner_id' field on Schema.table1 to 'SYSADMIN' for FISCAL, BREAKDWN, and PROGRAM table types.
--

v_code          number;
v_errm          varchar2(512);
vOM_Recipients  VARCHAR2(200) := 'personal_email@email.com';

begin
update Schema.table1
set owner_id = 'SYSADMIN'
where table_type in ('FISCAL','BREAKDWN','PROGRAM') and owner_id <> 'SYSADMIN';
commit;

EXCEPTION
  WHEN OTHERS THEN
     v_code := SQLCODE;
     v_errm := SUBSTR(SQLERRM, 1 , 512);
     UTL_MAIL.send(sender => 'automated_email@email.com',
                             recipients => vOM_Recipients,
                                   subject => 'Procedure_1 Job Error',
     message => 'Procedure_1 Job Error.' || chr(10) || chr(10) ||
     'The error code is '||v_code||'-'||v_errm||' ');
end;

1 个答案:

答案 0 :(得分:1)

Justin Cave的评论: 如果你只是想抛出一个随机错误raise_application_error(-20001,'Some Random Message');