/ Published in: SQL
stored procedure for executing batch sql statements
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
DROP TABLE IF EXISTS batch_sql_t; CREATE TABLE batch_sql_t ( id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL, operation VARCHAR(33) NOT NULL, order_number INTEGER NOT NULL, sql_statement VARCHAR(512) NOT NULL ); DROP PROCEDURE IF EXISTS run_batch_sql; delimiter $$ CREATE PROCEDURE run_batch_sql(IN sql_operation VARCHAR(22)) BEGIN DECLARE finished INTEGER; DECLARE dynamic_sql VARCHAR(512); DECLARE sql_cursor CURSOR FOR SELECT sql_statement FROM batch_sql_t t WHERE operation = sql_operation ORDER BY order_number; DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1; OPEN sql_cursor; SET finished = 0; read_loop: loop fetch sql_cursor INTO dynamic_sql; IF finished = 1 THEN leave read_loop; END IF; SET @query := dynamic_sql; -- select @query; PREPARE prep_sql FROM @query; EXECUTE prep_sql; deallocate PREPARE prep_sql; END loop read_loop; close sql_cursor; DELETE FROM batch_sql_t WHERE operation = sql_operation; END $$ delimiter ;