Skip to content

Instantly share code, notes, and snippets.

@rseon
Created December 17, 2019 16:10
Show Gist options
  • Save rseon/529b63e842ca228e871667d977adb4c8 to your computer and use it in GitHub Desktop.
Save rseon/529b63e842ca228e871667d977adb4c8 to your computer and use it in GitHub Desktop.
MySQL Storred Procedure example
CREATE PROCEDURE `MyProcedure`(
/*IN `in_field_a` INT,
IN `in_field_b` VARCHAR(255)*/
)
BEGIN
/*DECLARE a_variable BOOLEAN DEFAULT TRUE;
DECLARE b_variable DATE DEFAULT NULL;
SET b_variable = NOW();*/
-- Cursor example
block_cursor: BEGIN
DECLARE done BOOLEAN DEFAULT 0;
DECLARE _cur_field_a VARCHAR(255) DEFAULT NULL;
DECLARE _cur_field_b DATE DEFAULT NULL;
DECLARE data_cursor CURSOR FOR
SELECT field_a, field_b
FROM my_table
;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN data_cursor;
REPEAT
FETCH data_cursor INTO _cur_field_a, _cur_field_b;
IF NOT done THEN
/*IF IFNULL(_cur_field_a, '') = '' AND b_variable > _cur_field_b THEN
SET a_variable = FALSE;
END IF;*/
SET done = 0;
END IF;
UNTIL done END REPEAT;
CLOSE data_cursor;
END block_cursor;
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment