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






Return NUMERIC(8,2) from a function

  
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>

          
  








Related examples in the same category

1.Return value from a function
2.Return integer value from a function
3.Check function parameter and return value
4.Using the returning value from a user-defined function in order by clause
5.Returning Decimal from the function
6.function returnes int