Custom MySQL Functions

Source: Internet
Author: User

1. mysql Create function syntax:

= {User | Current_User}] FUNCTION sp_name ([func_parameter[,...]) RETURNS type[characteristic ...] routine_bodyfunc_parameter:    param_name typetype: Any    valid MySQL data Typeroutine_body:    Valid SQL Routine statement

Note: custom functions cannot be used across libraries

Mysql> DELIMITER//MySQL>CREATE FUNCTION ver_compare (n int, M int)-RETURNSVARCHAR ( -) #声明返回的数据类型-BEGIN-DECLARE S VARCHAR ( -); IF n = m then SET s ='equals'; -ELSEIF n > m then SET s ='Greater'; ELSE SET s =' Less'; -END IF; SET s = CONCAT (' is', S,'than'); -END IF; SET s = CONCAT (n,' ', S,' 'M'.'); -RETURN S; #一个实际的返回值
-END//MySQL> DELIMITER;

From the above example, it is not difficult to see that the creation of a custom function is actually similar to the Create stored procedure.

2. Function call

Custom functions are similar to stored procedures in creating definitions, but are slightly different when called.

 mysql> call Ver_compare (1 , 2  ); ERROR  1305  (42000  ): PROCEDURE db1. Ver_compare does not existmysql  > set  @ax = ver_compare (1 , 2   > Span style= "COLOR: #0000ff" >select   @ax;  +-------------------+| @ax |+-------------------+| 1  is  less than 2 . |+-------------------+  

when we call the function, function needs to appear to the right of = (that is, the calling function needs to have a variable to receive its result )

3. Other

> view All custom functions:show function status\g;

> Delete specified custom function:drop function func_name;

Custom MySQL functions

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.