You can use the CREATE PROCEDURE statement to create a stored procedure.
The database stored procedure syntax format is as follows:
CREATE PROCEDURE 过程名([[IN|OUT|INOUT] 参数名 数据类型[,[IN|OUT|INOUT] 参数名 数据类型…]]) [特性 ...] 过程体 DELIMITER // CREATE PROCEDURE myproc(OUT s int) BEGIN SELECT COUNT(*) INTO s FROM students; END // DELIMITER ;
Example: Create a simple stored procedure
-- ---------------------------- -- Procedure structure for `proc_adder` -- ----------------------------DROP PROCEDURE IF EXISTS `proc_adder`; DELIMITER ;;CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_adder`(IN a int, IN b int, OUT sum int)BEGIN #Routine body goes here... DECLARE c int; if a is null then set a = 0; end if; if b is null then set b = 0; end if;set sum = a + b;END ;; DELIMITER ;
Execute the above stored results and verify whether they are correct, as shown below
set @b=5; call proc_adder(2,@b,@s); select @s as sum;
The above is the detailed content of How to write mysql stored procedure. For more information, please follow other related articles on the PHP Chinese website!