How do I save data and then raise an exception?

From: "Rob Richardson" <Rob(dot)Richardson(at)rad-con(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: How do I save data and then raise an exception?
Date: 2008-10-02 20:25:13
Message-ID: 04A6DB42D2BA534FAC77B90562A6A03DA95EB7@server.rad-con.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alvaro Herrera 2008-10-03 01:44:16 Re: How do I save data and then raise an exception?
Previous Message Frank Durstewitz, Emporis GmbH 2008-10-02 19:39:34 Re: Trigger disable for table