Save the following stored procedure script in a text file.
drop procedure if exists dorepeat; DELIMITER // CREATE PROCEDURE dorepeat(IN p1 INT, OUT x INT) BEGIN SET x = 0; REPEAT SET x = x + 1; UNTIL x > p1 END REPEAT; END // DELIMITER ;
Create the stored procedure as follows:
$ mysql -D test -u me -p <dorepeat.sql Enter password: ******
Here, interactively connect to the database and execute the stored procedure using '@x' an automatic variable.
$ mysql -u me -p Enter password: ****** mysql> connect test; mysql> call dorepeat(4, @x); Query OK, 0 rows affected (0.00 sec) mysql> select @x; +-------+ | @x | +-------+ | 5 | +-------+ 1 row in set (0.00 sec)