Call another function : Call « Procedure Function « SQL / MySQL






Call another function

  
mysql>
mysql> delimiter $$
mysql>
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>
mysql> CREATE PROCEDURE myProc(aNumber int)
    -> Begin
    ->      IF (isodd(aNumber)) THEN
    ->           SELECT ' is odd';
    ->      ELSE
    ->           SELECT ' is even';
    ->      END IF;
    -> END$$
Query OK, 0 rows affected (0.02 sec)

mysql>
mysql> delimiter ;
mysql>
mysql>
mysql> call myProc(2);
+----------+
| is even  |
+----------+
|  is even |
+----------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> call myProc(1);
+---------+
| is odd  |
+---------+
|  is odd |
+---------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> call myProc(4);
+----------+
| is even  |
+----------+
|  is even |
+----------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>
mysql> drop procedure myProc;
Query OK, 0 rows affected (0.00 sec)

mysql> drop function isodd;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>

          
  








Related examples in the same category

1.Call another procedure
2.Call another procedure with parameter
3.Call another procedure to pass the error code
4.Nested function call
5.Call a Procedure