1696
MySQLERRORNotableStored RoutinesHIGH confidence

Cannot execute FLUSH statement in stored function or trigger

Production Risk

Low — routine creation or execution fails.

What this means

FLUSH statements (FLUSH TABLES, FLUSH PRIVILEGES, etc.) are not permitted inside stored functions or triggers because they implicitly commit the current transaction.

Why it happens
  1. 1Calling FLUSH inside a stored function or trigger body.
How to reproduce
trigger — this will error
trigger — this will error
CREATE FUNCTION bad_flush() RETURNS INT
BEGIN
  FLUSH TABLES;
  RETURN 1;
END;

expected output

ERROR 1696 (HY000): Cannot execute the given command because you have active locked tables or an active transaction.

Fix

Move FLUSH outside the stored routine

Move FLUSH outside the stored routine
-- Call FLUSH from application layer or a separate stored procedure called outside transactions.

Why this works

FLUSH requires no active transaction context; it must be called at the session level.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 1696 ER_FLUSHES_NOT_POSSIBLE

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MySQL errors