Skip to content

Instantly share code, notes, and snippets.

@ivancp
Last active August 29, 2015 14:02
Show Gist options
  • Save ivancp/08ebd11bc3798fda25a3 to your computer and use it in GitHub Desktop.
Save ivancp/08ebd11bc3798fda25a3 to your computer and use it in GitHub Desktop.
-- http://bugs.mysql.com/bug.php?id=72838
use test;
DELIMITER $$
DROP PROCEDURE IF EXISTS sp_test$$
CREATE PROCEDURE `sp_test`(p_query varchar(250))
BEGIN
-- Declarations
declare var1 varchar(250); -- change as need
declare var2 varchar(250); -- change as need
declare no_more_rows bool default false;
DECLARE cursor1 CURSOR FOR select * from tmp_table;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rows = TRUE;
-- creating temporary table and open as cursor
drop temporary table if exists tmp_table;
SET @prepared_sql = concat('create temporary table tmp_table ',p_query);
PREPARE stmt FROM @prepared_sql;
EXECUTE stmt;
OPEN cursor1;
-- Business Logic
cursor_loop: LOOP
fetch cursor1 into var1, var2;
IF no_more_rows THEN
LEAVE cursor_loop;
END IF;
-- DO SOMETHING WITH var1,var2
END LOOP cursor_loop;
-- closing cursors
CLOSE cursor1;
DEALLOCATE PREPARE stmt;
-- select only for test purposes
select * from tmp_table;
drop temporary table tmp_table;
END$$
DELIMITER ;
call sp_test('select table_name, table_collation from information_schema.tables where table_schema = \'mysql\';');
call sp_test('select table_collation, table_name from information_schema.tables where table_schema = \'mysql\';');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment