I found a JSON based approach that works with the latest MySQL / MariaDB systems. Check out the link below (original author Federico Razzoli): https://federico-razzoli.com/variable-number-of-parameters-and-optional-parameters-in-mysql-mariadb-procedures
Essentially, you take the BLOB parameter, which is actually a JSON object, and then execute JSON_UNQUOTE (JSON_EXTRACT (json object, key)) depending on the situation.
I removed the statement here:
CREATE FUNCTION table_exists(params BLOB) RETURNS BOOL NOT DETERMINISTIC READS SQL DATA COMMENT ' Return whether a table exists. Parameters must be passed in a JSON document: * schema (optional). : Schema that could contain the table. By default, the schema containing this procedure. * table : Name of the table to check. ' BEGIN DECLARE v_table VARCHAR(64) DEFAULT JSON_UNQUOTE(JSON_EXTRACT(params, '$.table')); DECLARE v_schema VARCHAR(64) DEFAULT JSON_UNQUOTE(JSON_EXTRACT(params, '$.schema')); IF v_schema IS NULL THEN RETURN EXISTS ( SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = SCHEMA() AND TABLE_NAME = v_table ); ELSE RETURN EXISTS ( SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = v_schema AND TABLE_NAME = v_table ); END IF; END;
source share