Create MySQL function

In MySQL, function is different than procedure by returning the value and can be called within SELECT statement.

DETERMINISTIC means that the function always produces same results for the same input parameters.

CREATE FUNCTION `Sample`(parameter VARCHAR(10)) RETURNS TINYINT(3) UNSIGNED
    DETERMINISTIC
BEGIN
    DECLARE myfunctionID TINYINT UNSIGNED;
 
    IF parameter = 'sample1' THEN
		SET returnID = 1;
    ELSEIF parameter = 'sample2' THEN
		SET returnID = 2;
    ELSEIF parameter = 'sample3' THEN
		SET returnID = 3;
        END IF;
    END IF;
 
 RETURN myfunctionID;
END

Leave a Reply