Home > Database > Mysql Tutorial > How to write mysql stored procedure

How to write mysql stored procedure

Release: 2020-07-10 17:11:44
Original
19834 people have browsed it

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 ;
Copy after login

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 ;
Copy after login

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;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template