How do I save data and then raise an exception? - Mailing list pgsql-general

From Rob Richardson
Subject How do I save data and then raise an exception?
Date
Msg-id 04A6DB42D2BA534FAC77B90562A6A03DA95EB7@server.rad-con.local
Whole thread Raw
In response to Re: Trigger disable for table  ("Frank Durstewitz, Emporis GmbH" <frank.durstewitz@emporis.com>)
Responses Re: How do I save data and then raise an exception?  (Alvaro Herrera <alvherre@commandprompt.com>)
List pgsql-general
Greetings!
 
The people who originally wrote the system I'm trying to work with did not know as much as they should have about working with databases, so I'm stuck with the following situation:
 
The applicaton is written in C++ (MS Visual C++ 6, Windows XP, in case it matters).  At one point, a required check was not performed before data was saved.  I cannot change this part of the C++ code, so I have to perform the check in the database, and the insert query has to fail so that the application will see that something bad happened.  However, there's another query that gets performed before the one I have to check.  If the check fails, the earlier query has to be undone.  The only way I know to intentionally fail a query is to raise an exception.  However, raising an exception causes all earlier database changes to be undone.  How can I avoid that?
 
Here's what I need to do:
 
IF query_check_fails THEN
    UPDATE some_table SET some_value = 0 WHERE some_condition_is_true;
    RAISE EXCEPTION 'Look, you idiot, do it right next time!'; 
END;
 
I need the update to work, but I need to raise the exception so the C++ code recognizes the error.  How can I do both?
 
Thanks very much!
 
RobR

pgsql-general by date:

Previous
From: "Frank Durstewitz, Emporis GmbH"
Date:
Subject: Re: Trigger disable for table
Next
From: Alvaro Herrera
Date:
Subject: Re: How do I save data and then raise an exception?