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.

2 Answers 2

3

You can enclose the commands with BEGIN and END:

  DECLARE EXIT HANDLER FOR SQLEXCEPTION,NOT FOUND,SQLWARNING
    BEGIN
      ROLLBACK;
      SELECT 0;
    END;
Sign up to request clarification or add additional context in comments.

1 Comment

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

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.