Description\n-----------\n\nCompound statements can also be used outside of stored programs.\n\n``sql\ndelimiter |\nIF @have_innodb THEN\n  CREATE TABLE IF NOT EXISTS innodb_index_stats (\n    database_name    VARCHAR(64) NOT NULL,\n    table_name       VARCHAR(64) NOT NULL,\n    index_name       VARCHAR(64) NOT NULL,\n    last_update      TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n    stat_name        VARCHAR(64) NOT NULL,\n    stat_value       BIGINT UNSIGNED NOT NULL,\n    sample_size      BIGINT UNSIGNED,\n    stat_description VARCHAR(1024) NOT NULL,\n    PRIMARY KEY (database_name, table_name, index_name, stat_name)\n  ) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_bin STATS_PERSISTENT=0;\nEND IF|\nQuery OK, 0 rows affected, 2 warnings (0.00 sec)\n`\n\nUsing compound statements this way is subject to following limitations:\n\n Only BEGIN, IF, CASE, LOOP, WHILE, REPEAT statements may start a compound statement outside of stored programs.\n BEGIN must use the BEGIN NOT ATOMIC syntax (otherwise it'll be confused with BEGIN that starts a transaction).\n A compound statement might not start with a label.\n A compound statement is parsed completely—note "2 warnings" in the above example, even if the condition was false (InnoDB was, indeed, disabled), and the CREATE TABLE statement was not executed, it was still parsed and the parser produced "Unknown storage engine" warning.\n\nInside 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:\n\n`sql\nBEGIN NOT ATOMIC\n    DECLARE foo CONDITION FOR 1146;\n    DECLARE x INT DEFAULT 0;\n    DECLARE CONTINUE HANDLER FOR SET x=1;\n    INSERT INTO test.t1 VALUES ("hndlr1", val, 2);\n    END|\n`\n\nExample how to use IF:\n\n`sql\nIF (1>0) THEN BEGIN NOT ATOMIC SELECT 1; END ; END IF;;\n`\n\nExample of how to use WHILE loop:\n\n`sql\nDELIMITER |\nBEGIN NOT ATOMIC\n    DECLARE x INT DEFAULT 0;\n    WHILE x <= 10 DO\n        SET x = x + 1;\n        SELECT x;\n    END WHILE;\nEND|\nDELIMITER ;\n``\n\nURL: https://mariadb.com/docs/server/reference/sql-statements/programmatic-compound-statements/using-compound-statements-outside-of-stored-programs