Starting from MariaDB 10.1.1 compound statements can also be used outside of stored programs.
delimiter | IF @have_innodb THEN CREATE TABLE IF NOT EXISTS innodb_index_stats ( database_name VARCHAR(64) NOT NULL, table_name VARCHAR(64) NOT NULL, index_name VARCHAR(64) NOT NULL, last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, stat_name VARCHAR(64) NOT NULL, stat_value BIGINT UNSIGNED NOT NULL, sample_size BIGINT UNSIGNED, stat_description VARCHAR(1024) NOT NULL, PRIMARY KEY (database_name, table_name, index_name, stat_name) ) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_bin STATS_PERSISTENT=0; END IF| Query OK, 0 rows affected, 2 warnings (0.00 sec)
Note, that using compound statements this way is subject to following limitations:
BEGIN NOT ATOMIC
syntax (otherwise it'll be confused with BEGIN that starts a transaction). Inside a compound block first three limitations do not apply, one can use anything that can be used inside a stored program — including labels, condition handlers, variables, and so on:
BEGIN NOT ATOMIC DECLARE foo CONDITION FOR 1146; DECLARE x INT DEFAULT 0; DECLARE CONTINUE HANDLER FOR SET x=1; INSERT INTO test.t1 VALUES ("hndlr1", val, 2); END|
Example how to use IF
:
IF (1>0) THEN BEGIN NOT ATOMIC SELECT 1; END ; END IF;;
© 2019 MariaDB
Licensed under the Creative Commons Attribution 3.0 Unported License and the GNU Free Documentation License.
https://mariadb.com/kb/en/using-compound-statements-outside-of-stored-programs/