I took the code from this answer and added a loop so can execute multiple queries passed to the stored procedure via single string parameter.
Guessing this might not have much by way of real-world utility, but just sharing for the heckuvit. I thought to want it when repeatedly wanting to drop then create tables in stored procedures and functions while learning mysql and wasting lots of time chasing my tail before stumbling on to the realization that you can't do multiple statements using PREPARE/EXECUTE.
# mysql_splitprepareexecutemulti.sql
#
# combine codebits from these two pages:
# 1 - https://stackoverflow.com/questions/20371677/execute-multiple-semi-colon-separated-query-using-mysql-prepared-statement
# 2 - https://sebhastian.com/mysql-split-string/
#
# Not gonna bother validate the sql at all. May not even handle
# empty string, no delimiter on end, etc.
#
# Take query string with presumably 1 or more sql statements
# - split based on parm string, default to semicolon
# - loop through parm str sql, prepare each query then execute each.
#
#
##############################################
CREATE DEFINER=`root`@`localhost` PROCEDURE `mssp_execStrungQueries`(
IN str text,
IN delimiterChar CHAR(1)
)
BEGIN
SET @inputString = str;
WHILE LOCATE(delimiterChar,@inputString) > 1 DO
SET @queryString = SUBSTRING_INDEX(@inputString,delimiterChar,1);
PREPARE stmt FROM @queryString; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @inputString = REGEXP_REPLACE(@inputString, (
SELECT LEFT(@inputString, LOCATE(delimiterChar, @inputString))),'',1,1);
select SLEEP(1); #slow down during dev in case conjur infinite loop
END WHILE;
END
##############################################
##############################################
### test calls to the SP:
call mssp_execStrungQueries(';',';');
select 'test just semi colon after call' as afterCallSP;
call mssp_execStrungQueries('','');
select 'test empty str after call' as afterCallSP;
SET @str = CONCAT('select \'ONEthingAAA\' as oneCol');
call mssp_execStrungQueries(@str,';');
select 'Test 1 query NO SEMICOLON test after call.' as afterCallSP;
SET @str = CONCAT('select \'ONEthingAAA\' as oneCol;');
call mssp_execStrungQueries(@str,';');
select 'Test 1 query WITH semicolon test after call.' as afterCallSP;
SET @str = CONCAT('select \'ONEthingAAA\' as oneCol;'
,'select \'TWOthingBBB\' as twoCol;'
,'select \'TREEtingCCCC\' as threeCol;');
call mssp_execStrungQueries(@str,';');
select 'Test 3 statements after call' as afterCallSP;
SET @str = CONCAT('select \'ONEthingAAA\' as oneCol;'
,'select \'TWOthingBBB\' as twoCol;'
,'select \'TREEtingCCCC\' as threeCol');
call mssp_execStrungQueries(@str,';');
select 'Test 3 stmts NO SEMI on last' as afterCallSP;
SET @str = CONCAT('select \'ONEthingAAA\' as oneCol;'
,'select \'TWOthingBBB\' as twoCol '
,'select \'TREEtingCCCC\' as threeCol;');
call mssp_execStrungQueries(@str,';');
select 'Test 3 stmts NO SEMI on 2nd' as afterCallSP;