当前位置:Gxlcms > 数据库问题 > Mysql存储过程

Mysql存储过程

时间:2021-07-01 10:21:17 帮助过:24人阅读

drop procedure if exists proc_addre; delimiter ;; ## 把默认的输入的结束符;替换成;; ## 创建一个 求和的 存储过程 ## DEFINER 创建者 可以省略 DEFINER=root@localhost 默认当前登录人 CREATE DEFINER=root@localhost PROCEDURE proc_addre(IN a int, IN b int, OUT sum int) BEGIN 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 ; SET @b=5; ## 定义变量 b=5 CALL proc_addre(2, @b, @s); ## 运行存储过程 SELECT @s as sum; ## 打印 s 变量

技术分享

 3、IF语句

## 存储过程 IF 语句
DROP     PROCEDURE IF EXISTS proce_if;
delimiter ;;
CREATE PROCEDURE porce_if (IN type INT)
BEGIN ## begin end; 是存储过程的块标示方法,类似PHP函数中的 { }  
    declare c VARCHAR(100); ## 定义一个 变长字符串 c
    IF type = 0 THEN 
        SET c = param is 0;
    ELSEIF type = 1 THEN
            SET c = param is 1;
    ELSE 
            set c = param is others, not 0 or 1;
    END IF;
    SELECT c ;
END;;    
delimiter;
CALL porce_if(1);

技术分享

 4、 case语句

## 存储过程 case 语句 感觉有点像 PHP 中的 switch 语句
DROP     PROCEDURE IF EXISTS proce_case;
delimiter ;;
CREATE PROCEDURE proce_case (IN type INT)
BEGIN ## begin end; 是存储过程的块标示方法,类似PHP函数中的 { }  
    declare c VARCHAR(100); ## 定义一个 变长字符串 c
    CASE type 
        WHEN 0 THEN    
            SET c = param is 0;
        WHEN 1 THEN
            SET c = param is 1;
        ELSE 
            set c = param is others, not 0 or 1;
    END CASE;
    SELECT c;
END;;    
delimiter;
CALL proce_case(1);

技术分享

 

Mysql存储过程

标签:begin   存储   运行   ges   函数   默认   oca   fine   log   

人气教程排行