Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading MySQL stored functions

When I try to create stored functions with the same names but different signatures:

CREATE FUNCTION `max`(a INT, b INT) RETURNS INT
BEGIN
    IF a > b THEN RETURN a; ELSE RETURN b; END IF;
END

CREATE FUNCTION `max`(a DATE, b DATE) RETURNS DATE
BEGIN
    IF a > b THEN RETURN a; ELSE RETURN b; END IF;
END

MySQL gives me an error:

FUNCTION max already exists

Do I really have to give distinct names to the functions like max_tinyint, max_int, max_date, max_datetime, etc.?

Even easier would be if I could create a function that accepts arbitrary types as parameters.

like image 684
AndreKR Avatar asked Jan 12 '12 03:01

AndreKR


1 Answers

mysql doesn't support function overloading. So the only way for you is to create functions with different names

UPD: and, yes, mysql has GREATEST() function to do what you want

like image 116
zerkms Avatar answered Sep 28 '22 06:09

zerkms