Home  >  Article  >  Database  >  A brief introduction to mysql custom functions

A brief introduction to mysql custom functions

怪我咯
怪我咯Original
2017-04-01 10:44:481135browse

Due to work needs, I need to write a custom number of rows in mysql, as follows
DELIMITER $$
DROP FUNCTION IF EXISTS `onlineFunction`$$
CREATE FUNCTION `onlineFunction`(rrrr VARCHAR(50)) RETURNS VARCHAR(255)
BEGIN
IF(rrrr='online') THEN RETURN 'online';END IF;
END$$
DELIMITER ;
The first line DELIMITER defines an end identifier, because MySQL uses a semicolon as the terminator of an SQL statement by default, and a semicolon is used inside the function body, so it will be followed by The default SQL terminator conflicts, so you need to first define another symbol as the SQL terminator. If this definition is not added...
Error code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'end' at line 1

The second line is Delete the class with the same name, otherwise...
Error code: 1304
FUNCTION onlineFunction already exists

The third line A function name, function variable , and return type

The fourth line begin is the start, which corresponds to end$$

The fifth line is the if judgment statement , the format is
if(...) then
....;
elseif
....;
else
....;
end if;
return ..;
Sometimes mysql cannot create custom functionbecause the function 2 is not turned on

Enter show variables like '%func% '; The command

will see the status of log_bin_trust_function_creators. If it is OFF, it means that the custom function is turned off.

Enter the command set global log_bin_trust_function_creators=1;

to set log_bin_trust_function_creators Turn on the custom function function

But this setting is a temporary solution, because the status will change to OFF after mysql automatically restarts, so you need to add "--" when the service starts

log-bin-trust-function-creators=1 ” parameter.

Or add log-bin-trust-function-creators=1 to the [mysqld] section in my.ini (my.cnf).


The above is the detailed content of A brief introduction to mysql custom functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn