Create a function in MySQL

Source: Internet
Author: User

MySQL create function one, to see whether the function of creating a function is turned on:
Mysql> Show variables like '%func% ';
+-----------------------------------------+-------+
| variable_name | Value |
+-----------------------------------------+-------+
| log_bin_trust_function_creators | On |
+-----------------------------------------+-------+
1 row in Set (0.02 sec)
Second, if value is off, you need to turn it on.
mysql> set global Log_bin_trust_function_creators=1; Third, when creating a function, select the database First,
mysql> use XXX;
Database changed
Delimiter $$ is to set $$ as the command termination symbol, in place of the semicolon, because the semicolon is used in begin...end;
Mysql> delimiter $$
CREATE FUNCTION First_func (param1 varchar (5), parmam2 varchar (5), param3 varchar (10))
RETURNS TINYINT
BEGIN
RETURN 1;
END
After the function is created, you need to restore the semicolon to the command termination symbol.
Mysql> delimiter; Four, test:
Mysql> Select First_func (' AAA ', ' BBB ', ' CCC ');
+-------------------------------+
| First_func (' AAA ', ' BBB ', ' CCC ') |
+-------------------------------+
| 1 |
+-------------------------------+
1 row in Set (0.47 sec) v. Delete function:
mysql> drop function First_func;
Query OK, 0 rows affected (0.11 sec) Vi. viewing functions
1) Show function status
Displays basic information about all functions in the database
2) View a specific function

Mysql>show Create function function;

Above: http://blog.csdn.net/tfhui928/article/details/6058074

Example:

--see if the CREATE function function is turned on
SHOW VARIABLES like '%func% ';

--Open the CREATE FUNCTION function and set Variable_name to 1
SET GLOBAL Log_bin_trust_function_creators=1;

--View all functions in the database
SHOW FUNCTION STATUS;

--See the specific function
SHOW CREATE FUNCTION Test_func

--delete function
DROP FUNCTION Test_func

--Create a query function
DELIMITER $$
CREATE FUNCTION Test_func (param1 VARCHAR), param2 int,param3 CHAR (5))
RETURNS INT
BEGIN
DECLARE ret_val int;--Defining variables
SELECT MAX (ID) into ret_val from test;
RETURN Ret_val;

END

--Execution function
SELECT test_func (' var ', ' n ', ' char ');

--Create an assignment function
DELIMITER $$
CREATE FUNCTION test_func1 (param1 int,param2 VARCHAR (20))
RETURNS INT
BEGIN
DECLARE Return_val INT;
DECLARE Val INT DEFAULT 2;
IF Val>1 Then
SET return_val = val;
ELSE
SET return_val = 1;
END IF;
RETURN Return_val;
END

--Execution function
SELECT test_func1 (1, ' admin ');

Transferred from: http://my.oschina.net/u/1273696/blog/181995

Create a function in MySQL

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.