1. Create store procedure
DELIMITER //
CREATE PROCEDURE Sp_SimpleCalculator(
IN num1 DECIMAL(10, 2),
IN num2 DECIMAL(10, 2),
IN operator CHAR(1),
OUT result DECIMAL(10, 2)
)
BEGIN
CASE
WHEN operator = '+' THEN SET result = num1 + num2;
WHEN operator = '-' THEN SET result = num1 - num2;
WHEN operator = '*' THEN SET result = num1 * num2;
WHEN operator = '/' THEN SET result = num1 / num2;
ELSE SET result = NULL;
END CASE;
END //
DELIMITER ;
2. result
CALL SimpleCalculator(100,20, '+', @result);
SELECT @@hostname,@@version,@result;
CALL SimpleCalculator(100,20, '-', @result);
SELECT @@hostname,@@version,@result;
CALL SimpleCalculator(100,400, '-', @result);
SELECT @@hostname,@@version,@result;
CALL SimpleCalculator(100,400, '*', @result);
SELECT @@hostname,@@version,@result;
CALL SimpleCalculator(100,400, '/', @result);
SELECT @@hostname,@@version,@result;
3. Drop store procedure if you want
drop procedure Sp_SimpleCalculator;
No comments:
Post a Comment