0

I have a script that starts with "START TRANSACTION", alters tables, inserts in others, and ends with "COMMIT"

However, I am executing this script using:

mysql -u root --password="jajaja" my_schema < ./my_script.sql

The problem is that if there is an error in any line mysql exits and the transaction is not rollback. If I use --force then the execution continues to the "COMMIT" but it did not rollback on error.

How can I tell mysql to perform a rollback if an error happened while executing my script?

QLands
  • 2,424
  • 5
  • 30
  • 50
  • The `mysql` command-line client is supposed to rollback if it exits without committing. See https://stackoverflow.com/questions/6121917/automatic-rollback-if-commit-transaction-is-not-reached – Barmar Jun 08 '23 at 21:22
  • `mysql --init-command=BEGIN -u root ...` – ysth Jun 08 '23 at 22:37
  • Unless you are using MySQL 8.0, `ALTER` terminates any open transactions. – Rick James Jun 10 '23 at 01:24

0 Answers0