Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a function with optional arguments in MySQL

Tags:

I want to create a function with optional arguments in MySQL. For instance, I want to create function that calculates the average of its arguments. I create a function of five arguments, but when user passes just two arguments to the function then it should still run and return the average of the two arguments.

like image 401
Krunal Avatar asked Feb 03 '11 11:02

Krunal


People also ask

How do you make an optional argument a function?

You can assign an optional argument using the assignment operator in a function definition or using the Python **kwargs statement. There are two types of arguments a Python function can accept: positional and optional. Optional arguments are values that do not need to be specified for a function to be called.

How do you create a function in MySQL?

The syntax to create a function in MySQL is: CREATE FUNCTION function_name [ (parameter datatype [, parameter datatype]) ] RETURNS return_datatype BEGIN declaration_section executable_section END; function_name.

Can SQL functions have optional parameters?

The trick that enables a work around for declaring optional parameters for t-sql functions is checking the parameter with ISNULL() within the sql function definition and calling the function with NULL values where you want to use default value for the optional parameter.

What is an optional argument in functions?

Optional arguments enable you to omit arguments for some parameters. Both techniques can be used with methods, indexers, constructors, and delegates. When you use named and optional arguments, the arguments are evaluated in the order in which they appear in the argument list, not the parameter list.


2 Answers

You cannot set optional parameters in MySQL stored procedures.
You can however set optional parameters in a MySQL UDF.

You do know that MySQL has an AVG aggregate function?

Workaround If you can face the ugliness of this workaround here's samplecode that uses a comma separated string with values as input and returns the average.

DELIMITER $$  CREATE FUNCTION MyAvg(valuestr varchar) RETURNS float BEGIN   DECLARE output float;   DECLARE arg_count integer;   DECLARE str_length integer;   DECLARE arg float;   DECLARE i integer;    SET output = NULL;    SET i = LENGTH(valuestr);   IF i > 0 THEN BEGIN       SET arg_count = 1;     WHILE i > 0 DO BEGIN       IF MID(valuestr, i, 1)       SET i = i - 1;     END; END WHILE;      /* calculate average */     SET output = 0;     SET i = arg_count;     WHILE i > 0 DO BEGIN       SET arg = SUBSTRING_INDEX(                    SUBSTRING_INDEX(valuestr, ',' , i)                   , ',', -1 );       SET output = output + arg;       SET i = i - 1;      END; END WHILE;            SET output = output / arg_count;    END; END IF;       RETURN output; END $$  DELIMITER ; 

Use concat_ws to feed the function.

SELECT MyAvg(CONCAT_WS(',',100,200,300,500)) AS test; 

You can also write an UDF in C(++) or Delphi/Lazarus

like image 198
Johan Avatar answered Sep 18 '22 07:09

Johan


While far from an ideal solution, here's how I solved optional parameters for a concat function I needed:

delimiter || create function safeConcat2(arg1 longtext, arg2 varchar(1023))  returns longtext  return safeConcat3(arg1, arg2, ''); ||  create function safeConcat3(arg1 longtext, arg2 varchar(1023), arg3 varchar(1023))  returns longtext  return safeConcat4(arg1, arg2, arg3, ''); ||  create function safeConcat4(arg1 longtext, arg2 varchar(1023), arg3 varchar(1023), arg4 varchar(1023)) returns longtext   begin       declare result longText;       set result = concat(arg1, arg2, arg3, arg4);       if( result is null) then           set result=arg1;       end if;       return result;   end || 

Note: This means you have to call the method that corresponds to the number of args.

like image 41
Ryan Shillington Avatar answered Sep 21 '22 07:09

Ryan Shillington