Syntax\n------\n\nPREPARE stmt_name FROM preparable_stmt\n\nDescription\n-----------\n\nThe PREPARE statement prepares a statement and assigns it a name,stmt_name, by which to refer to the statement later. Statement names are not case sensitive. preparable_stmt is an expression with the text of the statement. It cannot contain stored function calls or subqueries. The text must represent a single SQL statement, not multiple statements. Within the statement, ? characters can be used as parameter markers to indicate where data values are to be bound to the query later when you execute it. The ? characters should not be enclosed within quotes, even if you intend to bind them to string values. Parameter markers can be used only where expressions should appear, not for SQL keywords, identifiers, and so forth.\n\nThe scope of a prepared statement is the session within which it is created. Other sessions cannot see it.\n\nIf a prepared statement with the given name already exists, it is deallocated implicitly before the new statement is prepared. This means that if the new statement contains an error and cannot be prepared, an error is returned and no statement with the given name exists.\n\nPrepared statements can be PREPARE and EXECUTE in a stored procedure, but not in a stored function or trigger. Also, even if the statement is prepared with PREPARE in a procedure, it will not be deallocated when the procedure execution ends.\n\nA prepared statement can access user-defined variables, but not local variables or procedure's parameters.\n\nIf the prepared statement contains a syntax error, PREPARE will fail. As a side effect, stored procedures can use it to check if a statement is valid. For example:\n\n``sql\nCREATE PROCEDURE test_stmt(IN sql_text TEXT)\nBEGIN\n        DECLARE EXIT HANDLER FOR SQLEXCEPTION\n        BEGIN\n                SELECT CONCAT(sql_text, ' is not valid');\n        END;\n        SET @SQL := sql_text;\n        PREPARE stmt FROM @SQL;\n        DEALLOCATE PREPARE stmt;\nEND;\n`\n\nThe FOUND_ROWS() and ROW_COUNT() functions, if called immediately after EXECUTE, return the number of rows read or affected by the prepared statements; however, if they are called after DEALLOCATE PREPARE, they provide information about this statement. If the prepared statement produces errors or warnings, GET DIAGNOSTICS return information about them. DEALLOCATE PREPARE shouldn't clear the diagnostics area, unless it produces an error.\n\nA prepared statement is executed with EXECUTE and released with DEALLOCATE PREPARE.\n\nThe max_prepared_stmt_count server system variable determines the number of allowed prepared statements that can be prepared on the server. If it is set to 0, prepared statements are not allowed. If the limit is reached, an error similar to the following will be produced:\n\n`sql\nERROR 1461 (42000): Can't create more than max_prepared_stmt_count statements \n  (current value: 0)\n`\n\nOracle Mode\n\nIn Oracle mode, PREPARE stmt FROM 'SELECT :1, :2' is used instead of ?.\n\nPermitted Statements\n\nThe following is valid from MariaDB 10.6.2.\n\nAll statements can be prepared, except PREPARE, EXECUTE, and DEALLOCATE / DROP PREPARE.\n\nPrior to this, not all statements can be prepared. Only the following SQL commands are permitted:\n\n ALTER TABLE\n ANALYZE TABLE\n BINLOG\n CACHE INDEX\n CALL\n CHANGE MASTER\n CHECKSUM {TABLE | TABLES}\n COMMIT\n {CREATE | DROP} DATABASE\n {CREATE | DROP} INDEX\n {CREATE | RENAME | DROP} TABLE\n {CREATE | RENAME | DROP} USER\n {CREATE | DROP} VIEW\n DELETE\n DESCRIBE\n DO\n EXPLAIN\n FLUSH {TABLE | TABLES | TABLES WITH READ LOCK | HOSTS | PRIVILEGES | LOGS | STATUS |\\\n  MASTER | SLAVE | DES_KEY_FILE | USER_RESOURCES | QUERY CACHE | TABLE_STATISTICS |\\\n  INDEX_STATISTICS | USER_STATISTICS | CLIENT_STATISTICS}\n GRANT\n INSERT\n INSTALL {PLUGIN | SONAME}\n HANDLER READ\n KILL\n LOAD INDEX INTO CACHE\n OPTIMIZE TABLE\n REPAIR TABLE\n REPLACE\n RESET {MASTER | SLAVE | QUERY CACHE}\n REVOKE\n ROLLBACK\n SELECT\n SET\n SET GLOBAL SQL_SLAVE_SKIP_COUNTER\n SET ROLE\n SET SQL_LOG_BIN\n SET TRANSACTION ISOLATION LEVEL\n SHOW EXPLAIN\n SHOW {DATABASES | TABLES | OPEN TABLES | TABLE STATUS | COLUMNS | INDEX | TRIGGERS |EVENTS | GRANTS | CHARACTER SET | COLLATION | ENGINES | \\PLUGINS [SONAME] | PRIVILEGES |PROCESSLIST | PROFILE | PROFILES | VARIABLES | STATUS | WARNINGS | ERRORS |TABLE_STATISTICS | INDEX_STATISTICS | USER_STATISTICS | CLIENT_STATISTICS | AUTHORS |CONTRIBUTORS}\n SHOW CREATE {DATABASE | TABLE | VIEW | PROCEDURE | FUNCTION | TRIGGER | EVENT}\n SHOW {FUNCTION | PROCEDURE} CODE\n SHOW BINLOG EVENTS\n SHOW SLAVE HOSTS\n SHOW {MASTER | BINARY} LOGS\n SHOW {MASTER | SLAVE | TABLES | INNODB | FUNCTION | PROCEDURE} STATUS\n SLAVE {START | STOP}\n TRUNCATE TABLE\n SHUTDOWN\n UNINSTALL {PLUGIN | SONAME}\n UPDATE\n\nSynonyms are not listed here, but can be used. For example, DESC can be used instead of DESCRIBE.\n\nCompound statements can be prepared too.\n\nNote that if a statement can be run in a stored routine, it will work even if it is called by a prepared statement. For example, SIGNAL can't be directly prepared. However, it is allowed in stored routines. If the x() procedure contains SIGNAL, you can still prepare and execute the 'CALL x();' prepared statement.\n\nPREPARE supports most kinds of expressions as well, for example:\n\n`sql\nPREPARE stmt FROM CONCAT('SELECT  FROM ', table_name);\n`\n\nWhen PREPARE is used with a statement which is not supported, the following error is produced:\n\n`sql\nERROR 1295 (HY000): This command is not supported in the prepared statement protocol yet\n``\n\nExamples\n--------\n\nCREATE TABLE t1 (a INT,b CHAR(10));\nINSERT INTO t1 VALUES (1,"one"),(2, "two"),(3,"three");\nPREPARE test FROM "select * from t1 where a=?";\nSET @param=2;\nEXECUTE test USING @param;\n+------+------+\n| a    | b    |\n+------+------+\n|    2 | two  |\n+------+------+\nSET @param=3;\nEXECUTE test USING @param;\n+------+-------+\n| a    | b     |\n+------+-------+\n|    3 | three |\n+------+-------+\nDEALLOCATE PREPARE test;\n\nURL: https://mariadb.com/docs/server/reference/sql-statements/prepared-statements/prepare-statement