mysql的call用法 call调用函数的例子

发布时间:2020-02-03编辑:脚本学堂
本文介绍下,mysql中call语句的用法,用call来调用函数的例子,供大家学习参考。

学习mysql中call语句调用函数的例子。
代码:

mysql> delimiter $$
mysql> CREATE FUNCTION isodd(input_number int) //创建函数
    ->        RETURNS int
    -> BEGIN
    ->         DECLARE v_isodd INT;
    ->
    ->         IF MOD(input_number,2)=0 THEN
    ->                 SET v_isodd=FALSE;
    ->         ELSE
    ->                 SET v_isodd=TRUE;
    ->         END IF;
    ->
    ->         RETURN(v_isodd);
    ->
    -> END$$
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE PROCEDURE myProc(aNumber int) //创建mysql存储过程
    -> Begin
    ->      IF (isodd(aNumber)) THEN
    ->           SELECT ' is odd';
    ->      ELSE
    ->           SELECT ' is even';
    ->      END IF;
    -> END$$
Query OK, 0 rows affected (0.02 sec)

mysql> delimiter ;
mysql> call myProc(2); //call 调用存储过程
+----------+
| is even  |
+----------+
|  is even |
+----------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

mysql> call myProc(1); //call 调用存储过程
+---------+
| is odd  |
+---------+
|  is odd |
+---------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> call myProc(4); //调用mysql存储过程
+----------+
| is even  |
+----------+
|  is even |
+----------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> drop procedure myProc; //删除mysql存储过程
Query OK, 0 rows affected (0.00 sec)

mysql> drop function isodd; //删除mysql函数
Query OK, 0 rows affected (0.00 sec)