[How-to] MySQL function example.
Scenario:
Guys, this is a simple tutorial on how to create a MySQL function.
For example, I need a MySQL function that will take in 2 integer input parameters and return a tinyint value to user.
Solution:
Here is the sample function code:
View Code MYSQL
DELIMITER $$ DROP FUNCTION IF EXISTS `sampleFunc1`$$ CREATE FUNCTION `sampleFunc1`( a INT, b INT ) RETURNS TINYINT(1) DETERMINISTIC BEGIN -- Function logic here DECLARE run TINYINT DEFAULT 0; IF (a+b) > 100 THEN SET run = 1; ELSE SET run = 2; END IF; RETURN run; END$$ DELIMITER ; |
5 Comments to “[How-to] MySQL function example.”
Leave a Reply
How do we pass an optional parameter with a default value as an argument to a stored function?
Hi Bianco,
So far base on my understanding, it is not possible to do so for MySQL.
You might need to send in the extra parameters for all your call function.
Thanks for this example.
@Biaco: Make a simple trick with if,else!
How to return a bunch of record from a function call?