码迷,mamicode.com
首页 > 数据库 > 详细

mysql存储过程

时间:2017-09-10 12:41:01      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:变量   into   cal   ase   drop   ima   arc   bool   iter   

1、 简单的存储过程创建

drop procedure if exists selectOrder;

DELIMITER //  
CREATE DEFINER=`root`@`localhost` PROCEDURE `selectOrder`()
BEGIN
    select * from order_h;
END
//

call selectOrder();

技术分享

2、 带入参的存储过程创建

drop procedure if exists selectOrder;

DELIMITER //  
CREATE PROCEDURE `selectOrder`(in customer varchar(32))
BEGIN
    select * from order_h where customerid=customer;
END
//

call selectOrder(ct9);

技术分享

3、 带出参的存储过程创建

drop procedure if exists selectOrder;

DELIMITER //  
CREATE PROCEDURE `selectOrder`(out money int)
BEGIN
    select sum(summoney) into money from order_h where customerid=ct9;
END
//

call selectOrder(@money);
SELECT @money FROM DUAL;

技术分享

4、 带有入参出参的存储过程创建

drop procedure if exists selectOrder;

DELIMITER //  
CREATE PROCEDURE `selectOrder`(in customer varchar(32), out money int)
BEGIN
    select sum(summoney) into money from order_h where customerid=customer;
END
//

call selectOrder(ct1, @money);
SELECT @money FROM DUAL;

技术分享

5、 存储过程控制语句

drop procedure if exists selectOrder;

DELIMITER //  
CREATE PROCEDURE `selectOrder`(in customer varchar(32), in ismax boolean, out money int)
BEGIN
    if ismax then
        select max(summoney) into money from order_h where customerid=customer;
    else
        select min(summoney) into money from order_h where customerid=customer;
    end if;
    /**
        其他语法
        申明变量 DECLARE s int;

        while循环
        WHILE i <= n DO
            set s = s + i;
        END WHILE;
        
        case when
        CASE type
        WHEN 0 THEN
            set c = ‘false‘;
        WHEN 1 THEN
            set c = ‘true‘;
        ELSE
            set c = ‘else‘;
        END CASE;
    */
END
//

call selectOrder(ct1, false, @money);
SELECT @money FROM DUAL;

技术分享

 

mysql存储过程

标签:变量   into   cal   ase   drop   ima   arc   bool   iter   

原文地址:http://www.cnblogs.com/coshaho/p/7500370.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!