Return NUMERIC(8,2) from a function : Function Returning « Procedure Function « MySQL Tutorial






mysql>
mysql>
mysql> delimiter $$
mysql> CREATE FUNCTION myFunction(normal_price NUMERIC(8,2))
    -> RETURNS NUMERIC(8,2)
    -> BEGIN
    ->
    ->     DECLARE discount_price NUMERIC(8,2);
    ->
    ->     IF (normal_price>500) THEN
    ->        SET discount_price=normal_price*.8;
    ->     ELSEIF (normal_price>100) THEN
    ->        SET discount_price=normal_price*.9;
    ->     ELSE
    ->        SET discount_price=normal_price;
    ->     END IF;
    ->     RETURN(discount_price);
    -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql>
mysql> select myFunction(123.123);
+---------------------+
| myFunction(123.123) |
+---------------------+
|              110.81 |
+---------------------+
1 row in set, 2 warnings (0.02 sec)

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

mysql>
mysql>








11.4.Function Returning
11.4.1.Return value from a function
11.4.2.Return integer value from a function
11.4.3.Return NUMERIC(8,2) from a function
11.4.4.Check function parameter and return value
11.4.5.Using the returning value from a user-defined function in order by clause
11.4.6.Returning Decimal from the function