2012-05-24 2 views
0

J'ai une procédure plsql pour supprimer des enregistrements des tables enfant et parent.Soulever Exception lorsque l'enregistrement enfant a été trouvé

Je souhaite lever une exception lorsque l'enregistrement enfant est trouvé.

Comment est-ce que je peux faire ceci?

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; 
/

Répondre

3

Supprimez le bloc d'exception si vous ne voulez pas intercepter l'exception!

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; 

Vous ne devez pas les attraper tous (exceptions are not pokemon). Si vous insistez vraiment sur la capture et sur-relançant vous pouvez utiliser RAISE ou 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; 

Si vous voulez être plus précis et un piège que l'exception des enfants, vous devrez define the exception numéro parce qu'il n'y a pas predefined exception dans PL/SQL pour cette erreur:

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; 
+0

est-il possible d'avoir quelque chose comme ça dans myproc? 'PRAGMA EXCEPTION_INIT (foreign_key_violated, -02291);' Pourriez-vous être assez aimable pour suggérer ce qui se passerait si commit existe dans une procédure? – user75ponic

+1

Bien sûr, même si je pense qu'une suppression va élever un 'ORA-02292', voir ma mise à jour. Pour en savoir plus sur les exceptions définies par l'utilisateur: http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/errors.htm#i3329 –

+0

Merci pour l'information. Apprécié. – user75ponic

1

I would like to raise exception when child record found

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: modifié ma réponse pour la lecture de déficit en compréhension, je pensais que vous vouliez et exception lorsque l'enregistrement des enfants non trouvé.

+0

Merci pour la réponse, la solution Vincent Malgrat a résolu mon problème résolu. Apprécié. – user75ponic

Questions connexes