Home  >  Article  >  Database  >  How to write mysql stored procedure

How to write mysql stored procedure

尚
Original
2019-06-19 11:35:1519795browse

How to write mysql stored procedure

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;

How to write mysql stored procedure

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn