dbTalk Databases Forums  

Exceptions when 0 rows affected.

comp.databases.postgresql.sql comp.databases.postgresql.sql


Discuss Exceptions when 0 rows affected. in the comp.databases.postgresql.sql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Stef
 
Posts: n/a

Default Exceptions when 0 rows affected. - 05-10-2004 , 08:08 AM






Hi all,

I've been looking on Google,the archives and documentation,
but cannot find what I'm looking for. Maybe I've read the
answer, but it's still evading me.

I'm working with postgres 7.3.4 using the psql client.

I want to know if it's possible to raise an error in a
transactional BEGIN->END block when e.g. an update
or delete statement affects zero rows.

I want the entire transaction block to rollback when
this happens. I can do it in perl and java , but would
in this instance like to load a file with SQL statements
using psql -f . Is it possible?

Kind Regards
Stefan

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iQCVAwUBQJ9+vLoLc+HYd7LFAQIBswP+KxEs7Vdjx6S9dlYOak LQEflMeuBWjs0L
Cb32VDBjvMIJTDJUgtlb7uWT2zgw2m35iwsvoWtQN6vLEbtUQI +huGnjreQw5SGT
xJ2ey4OWmCBBdr620n2R0TDBuAzb+jQSKQ4oTtbhmG69DhjKbh Go5EL2NbvWMwEO
WR+l8+p3U3E=
=uBNN
-----END PGP SIGNATURE-----


Reply With Quote
  #2  
Old   
Andrei Bintintan
 
Posts: n/a

Default Re: Exceptions when 0 rows affected. - 05-11-2004 , 01:02 AM






Hi Stef,

Your problem depends on what interface/programming language you're using.
In a Begin End transaction you will have to verify if the returned data is
good for you so then you decide if you run at the end "COMMIT" or
"ROLLBACK".

In php (and where is compatible) I suggest this:

//this is php code:

$error = false;
dbExec("BEGIN");

//I use a switch statement so I can jump out of it with a break at any time
switch (1){
case 1:
...
$sql = "Insert....";
if (!dbExec($sql)) {$error = true; break;}//was an error?? we jump out
from switch
...
$sql = "Select ....";
if (!dbExec($sql)) {$error = true; break;}
...
$sql = "Update....";
if (!dbExec($sql)) {$error = true; break;}
...
}//switch

if ($error) dbExec("ROLBACK"); //an error ocured
else dbExec("COMMIT");//there was no error

Hope this helps.

Best regards,
Andy.

----- Original Message -----
From: "Stef" <svb (AT) ucs (DOT) co.za>
To: <pgsql-sql (AT) postgresql (DOT) org>
Sent: Monday, May 10, 2004 4:08 PM
Subject: [SQL] Exceptions when 0 rows affected.

Hi all,

I've been looking on Google,the archives and documentation,
but cannot find what I'm looking for. Maybe I've read the
answer, but it's still evading me.

I'm working with postgres 7.3.4 using the psql client.

I want to know if it's possible to raise an error in a
transactional BEGIN->END block when e.g. an update
or delete statement affects zero rows.

I want the entire transaction block to rollback when
this happens. I can do it in perl and java , but would
in this instance like to load a file with SQL statements
using psql -f . Is it possible?

Kind Regards
Stefan




---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend


Reply With Quote
  #3  
Old   
Stef
 
Posts: n/a

Default Re: Exceptions when 0 rows affected. - 05-13-2004 , 09:49 AM




Andrei Bintintan mentioned :
=> Your problem depends on what interface/programming language you're using.

Yep, I tried to do it using rules or triggers, but I can't get it to do what I want
exactly, and it's not a good idea to put any rules or triggers on my database.

I'm sticking to psql though, and managed to create the query with a nested
nullif and coalesce to make my query fail if there are 0 rows matching for
an update or delete.

Cheers
Stef

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.