-- create stored procedure (once)
CREATE PROCEDURE execute_many_queries (queries_text TEXT)
BEGIN
REPEAT
SET @sql := SUBSTRING_INDEX(queries_text, ';', 1);
SET queries_text := TRIM(LEADING ';' FROM TRIM(LEADING @sql FROM queries_text));
PREPARE stmt FROM @sql;
EXECUTE stmt;
DROP PREPARE stmt;
UNTIL queries_text = '' END REPEAT;
END
-- create testing table
CREATE TABLE test (id INT, val INT);
-- execute 3 queries by 1 statement
CALL execute_many_queries ('INSERT INTO test VALUES (1,11), (2,22); UPDATE test SET val = 222 WHERE id = 2; SELECT * FROM test;');
id | val
-: | --:
1 | 11
2 | 222
-- execute more 2 queries by 1 statement
CALL execute_many_queries ('UPDATE test SET val = 111 WHERE id = 1; SELECT * FROM test;');
1条答案
按热度按时间ijnw1ujt1#
演示示例。
db<>在这里摆弄
小心使用!sp中没有检查-查询必须没有错误。注射是可能的。