-2

First, I set John to the user-defined variable @name as shown below:

SET @name = 'John';

Then, I set David to @name in a transaction, then rollbacked as shown below:

BEGIN;
SET @name = 'David';
ROLLBACK;

But, @name was not rollbacked to John as shown below:

mysql> SELECT @name;
+-------+
| @name |
+-------+
| David |
+-------+

I read the doc about transaction and user-defined variables but I could not find any relevant information.

So, how can I rollback user-defined variables?

2
  • Transactions effects only the tables whose engine is transactional. None other objects, including user-defined variables, are saved by transactional savepoints. Commented Dec 15, 2023 at 6:19
  • 1
    Why would you want to do this? Commented Dec 15, 2023 at 8:46

1 Answer 1

0

A user-defined variable is as long active as long the session is online and disappear, when you disconnect. It has a session scope

A transaction scope don't touch that and can't influence it, besides setting the value and it will not reverse it as it is putside of its scope

Sign up to request clarification or add additional context in comments.

Comments

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.