create_function_syntax.rst 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. .. _create_function_syntax:
  2. CREATE FUNCTION syntax
  3. ----------------------
  4. .. code-block:: none
  5. CREATE FUNCTION udf_name
  6. RETURNS {INT | INTEGER | BIGINT | FLOAT | STRING}
  7. SONAME 'udf_lib_file'
  8. CREATE FUNCTION statement installs a :ref:`user-defined function
  9. (UDF) <udfs_user_defined_functions>` with the given name
  10. and type from the given library file. The library file must reside in a
  11. trusted
  12. :ref:`plugin_dir <plugin_dir>`
  13. directory. On success, the function is available for use in all
  14. subsequent queries that the server receives. Example:
  15. .. code-block:: mysql
  16. mysql> CREATE FUNCTION avgmva RETURNS INTEGER SONAME 'udfexample.dll';
  17. Query OK, 0 rows affected (0.03 sec)
  18. mysql> SELECT *, AVGMVA(tag) AS q from test1;
  19. +------+--------+---------+-----------+
  20. | id | weight | tag | q |
  21. +------+--------+---------+-----------+
  22. | 1 | 1 | 1,3,5,7 | 4.000000 |
  23. | 2 | 1 | 2,4,6 | 4.000000 |
  24. | 3 | 1 | 15 | 15.000000 |
  25. | 4 | 1 | 7,40 | 23.500000 |
  26. +------+--------+---------+-----------+