James1012
James1012

Reputation: 21

mysql exit handler help

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.

Upvotes: 2

Views: 3147

Answers (2)

ChoNuff
ChoNuff

Reputation: 822

You can enclose the commands with BEGIN and END:

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

Upvotes: 3

soulmerge
soulmerge

Reputation: 75724

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.

Upvotes: 0

Related Questions