我有一个plsql过程来删除子表和父表中的记录。
我想在找到儿童记录时提出异常。
我该怎么做?
CREATE OR REPLACE PROCEDURE myproc(
p_id number,
p_id2 number,
p_par3 number)
AS
BEGIN
DELETE FROM child_table
WHERE id1 = p_id and par=p_par3;
DELETE FROM parent_table
WHERE no = p_id2;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
--raise
END myproc;
/
答案 0 :(得分:5)
如果您不想捕获异常,请删除异常块!
CREATE OR REPLACE PROCEDURE myproc(p_id NUMBER, p_id2 NUMBER, p_par3 NUMBER) AS
BEGIN
DELETE FROM child_table
WHERE id1 = p_id
AND par = p_par3;
DELETE FROM parent_table WHERE no = p_id2;
COMMIT; /* do you really want to commit in a procedure? */
END myproc;
您无需全部捕捉它们(exceptions are not pokemon)。如果您真的坚持要抓住并重新加注,可以使用RAISE或RAISE_APPLICATION_ERROR:
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001 /* user-defined exception number
between -20999 and -20000 */,
'your user-defined exception message',
TRUE /* this will preserve the error stack */
);
END;
如果您想要更具体并且仅捕获子异常,则必须define the exception编号,因为此错误在PL / SQL中没有predefined exception:
CREATE OR REPLACE PROCEDURE myproc(p_id NUMBER, p_id2 NUMBER, p_par3 NUMBER) AS
child_exists EXCEPTION;
PRAGMA EXCEPTION_INIT(child_exists, -2292); /* raises ORA-02292 */
BEGIN
DELETE FROM child_table
WHERE id1 = p_id
AND par = p_par3;
DELETE FROM parent_table WHERE no = p_id2;
COMMIT; /* do you really want to commit in a procedure? */
EXCEPTION
WHEN child_exists THEN
-- do something
RAISE;
END myproc;
答案 1 :(得分:1)
我想在找到儿童记录时提出异常
create or replace procedure myproc(p_id number, p_id2 number, p_par3 number)
as
begin
delete from child_table
where id1 = p_id
and par = p_par3;
if sql%rowcount > 0 then
raise_application_error(-20001, 'Child record(s) found!');
end if;
delete from parent_table where no = p_id2;
end myproc;
HTH。
的Alessandro
PS:修改了我的阅读理解缺陷的答案,我认为你没有找到儿童记录时你想要和异常。