tags:

views:

98

answers:

2

I have a simple update that will use a transaction. If the transaction fails, I want to rollback and send out an integer value of 0. Here is my code so far. It isn't working and all I get back is integer 1 even when it fails.

declare exit handler for not found rollback;
declare exit handler for sqlwarning rollback;
declare exit handler for sqlexception rollback;
set row = 0;

START TRANSACTION;
UPDATE user
SET name = name
WHERE code = code;
COMMIT;

set row = 1;

Does anyone have any ideas how I can fix this? Really, I'm open here. If someone has a better approach, I'm all ears.

A: 

I don't know if you just posted the wrong code, but your query is not generating any errors, you are overwriting x with x where y equals y, which is a NOP.

soulmerge
Thanks for the answer soulmerge. This is the code that I am using. Funny thing.. I am using this same code for an insert and it works perfect. I have looked over this code and compared it against the working code and there is no difference. I'm stumped.
James1012
A: 

You can enclose the commands with BEGIN and END:

DECLARE EXIT HANDLER FOR SQLEXCEPTION,NOT FOUND,SQLWARNING BEGIN ROLLBACK; SELECT 0; END;

Chosun
Sorry, the formatting didn't go through. The "BEGIN" is on a separate line, as is "rollback;", "select 0;" and "end;".
Chosun