A brief talk on Mysql custom function _mysql

Source: Internet
Author: User

Because work requires, write a custom number of MySQL lines, 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 a closing identifier, because MySQL defaults to the Terminator of the SQL statement, and the function body uses a semicolon, so it conflicts with the default SQL terminator, so you need to first define a different symbol as the end of SQL. Without adding this definition ...

Error code: 1064 You have a error in your SQL syntax check the manual this corresponds to
your MySQL server version for the Right syntax to use near ' end ' in line 1

The second line is to delete the class with the same name, or ...

Error code: 1304
FUNCTION onlinefunction already exists

Third line first function name, function variable, and return type

Line four begins with the beginning, corresponds to the end$$

Line Five is the IF Judgment statement, formatted as

if (...) then ...
;
ElseIf ...
;
else
...;
End If;
return ...;

Sometimes MySQL cannot create a custom function because the feature 2 is not turned on

Enter show variables like '%func% '; Command

You will see the state of the log_bin_trust_function_creators, and if it is off, the custom function function is turned off.

Input command set global Log_bin_trust_function_creators=1;

You can turn log_bin_trust_function_creators on the custom function function

But this setting is a temporary scenario, because the MySQL automatically restarts and the state turns off again, so you need to

Add the "--log-bin-trust-function-creators=1" parameter when the service starts.
Or add Log-bin-trust-function-creators=1 to the [mysqld] section in My.ini (MY.CNF).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.