Detailed explanation of MySQL custom function usage

A user-defined function UDF is an extension of MySQL with a new function that acts like an inherent (built-in) function like ABS() or CONCAT().

So UDF is an extension of MySQL functionality to

create and delete custom functions syntax:

Create UDF:

  CREATE [AGGREGATE] FUNCTION function_name(parameter_name type,[parameter_name type,...])

  RETURNS {STRING|INTEGER|REAL}

  runtime_body

simply That is:

  CREATE FUNCTION function name (parameter list)

  RETURNS return value type

  function body

Delete UDF:

  DROP FUNCTION function_name

call custom function Syntax:

  SELECT function_name(parameter_value,...)

Syntax example:

create a simple no-parameter UDF

CREATE FUNCTION simpleFun ()RETURNS VARVHAR(20) RETURN "Hello World!";

Description:

UDF can achieve more than that, UDF has two key points, one is parameter, the other is return value, UDF can have no parameters, but UDF must have and only one return value

In the function body, we can use more complex syntax, such as compound structure/flow control/any SQL statement/define variable, etc.

Compound structure definition syntax:

In the function body, if it contains multiple statements, we need to put multiple statements in into the BEGIN...END statement
block

DELIMITER //
CREATE FUNCTION IF EXIST deleteById(uid SMALLINT UNSIGNED)
RETURNS VARCHAR(20)
BEGIN
DELETE FROM son WHERE id = uid;
RETURN (SELECT COUNT(id) FROM son);
END //

Copy code

Modify the default terminator syntax:

DELIMITER // It means to modify the default terminator ";" to "//", and subsequent SQL statements must end with "//"

Special note: In

UDF, REURN Statements are also included in BEGIN...

END Define local variables in custom functions Syntax:

DECLARE var_name[,varname]...date_type [DEFAULT VALUE]; In

short:

DECLARE variable1[,variable2,... ] Variable type [DEFAULT default value]

The scope of these variables is in the BEGIN...END program, and the statement defining local variables must be defined on the first line of BEGIN...END

Example:
copy code

DELIMITER //
CREATE FUNCTION addTwoNumber(x SMALLINT UNSIGNED, Y SMALLINT UNSIGNED)
RETURNS SMALLINT
BEGIN
DECLARE a, b SMALLINT UNSIGNED DEFAULT 10;
SET a = x, b = y;
RETURN a+b;
END//

copy code

The above code just adds two numbers. Of course, there is no need to write this. It is just to explain the usage of local variables. It is still necessary to explain: the scope of these local variables is in the BEGIN...END program. Assignment syntax

for variables:

SET parameter_name = value[,parameter_name = value...]

SELECT INTO parameter_name

eg:

...in a UDF...
DECLARE x int;
SELECT COUNT(id) FROM tdb_name INTO x;
RETURN x;
END//

User Variable definition syntax: (can be understood as a global variable)

SET @param_name = value

SET @allParam = 100;
SELECT @allParam;

The @allParam user variable is defined and displayed above, and its scope is only valid for the client of the current

user . Flow control statement syntax in custom functions: Flow control

can be used in stored procedures and functions to control the execution of statements.

In MySQL, you can use IF statement, CASE statement, LOOP statement, LEAVE statement, ITERATE statement, REPEAT statement and WHILE statement to control the flow.

Each flow may contain a single statement, or a compound statement using the BEGIN...END construct, which can be nested

1. IF statement

IF statement is used to make conditional judgments. Depending on whether the condition is met, different statements will be executed. The basic form of its grammar is as follows:

IF search_condition THEN statement_list
[ELSEIF search_condition THEN statement_list] ...
[ELSE statement_list]
END IF

The search_condition parameter represents the condition judgment statement; the statement_list parameter represents the execution statement with different conditions.

Note: MYSQL also has an IF() function, which is different from the IF statement described here.

The following is an example of an IF statement. The code is as follows:

IF age>20 THEN SET @count1=@count1+1; 
ELSEIF age=20 THEN SET @count2=@count2+1; 
ELSE SET @count3=@count3+1; 
END IF;

This example executes different SET statements according to the size relationship between age and 20.

If the age value is greater than 20, then add 1 to the value of count1; if the age value is equal to 20, then add 1 to the value of count2;

otherwise, add 1 to the value of count3. IF statements need to use END IF to end.

2. CASE statement

CASE statement is also used for conditional judgment, which can realize more complex conditional judgment than IF statement. The basic form of CASE statement is as follows:

CASE case_value
WHEN when_value THEN statement_list
[WHEN when_value THEN statement_list] ...
[ELSE statement_list]
END CASE

Among them, the case_value parameter represents the variable of condition judgment; the

when_value parameter represents the value of the variable; the

statement_list parameter represents the different The execution statement for the when_value value.

There is another form of the CASE statement. The syntax of this form is as follows:

CASE
WHEN search_condition THEN statement_list
[WHEN search_condition THEN statement_list] ...
[ELSE statement_list]
END CASE

Among them, the search_condition parameter represents the condition judgment statement;

the statement_list parameter represents the execution statement with different conditions.

The following is an example of a CASE statement. The code is as follows:

CASE age
WHEN 20 THEN SET @count1=@count1+1;
ELSE SET @count2=@count2+1;
END CASE ;

The code can also be in the following form:

CASE
WHEN age=20 THEN SET @count1=@count1 +1;
ELSE SET @count2=@count2+1;
END CASE ;

In this example, if the age value is 20, the value of count1 is incremented by 1; otherwise, the value of count2 is incremented by 1. CASE statements must be terminated with END CASE.

    Note: The CASE statement here is slightly different from the CASE statement for the SQL CASE expression described in "Control Flow Functions". The CASE statement here cannot have an ELSE NULL clause

    and use END CASE instead of END to terminate! !



3. LOOP statement

The LOOP statement can cause certain specific statements to be executed repeatedly to achieve a simple loop.

However, the LOOP statement itself does not have a statement to stop the loop, and the loop must be stopped when a LEAVE statement is encountered.

The basic form of the syntax of the LOOP statement is as follows:

[begin_label:] LOOP
statement_list
END LOOP [end_label]

Among them, the begin_label parameter and the end_label parameter represent the signs of the start and end of the loop, respectively. These two signs must be the same and can be omitted;

the statement_list parameter indicates the statement that needs to be executed in a loop.

Below is an example of a LOOP statement. The code is as follows:

add_num: LOOP 
SET @count=@count+1; 
END LOOP add_num ;

This example loops through the operation of adding 1 to count. Because there is no statement to jump out of the loop, the loop becomes an infinite loop.

LOOP loops all end with END LOOP.



4. LEAVE statement

LEAVE statement is mainly used to jump out of loop control. Its grammatical form is as follows:

LEAVE label

Among them, the label parameter represents the sign of the loop.



Below is an example of a LEAVE statement. The code is as follows:

add_num: LOOP
SET @count=@count+1;
IF @count=100 THEN
LEAVE add_num ;
END LOOP add_num ;

This example loops and executes the operation of adding 1 to count. When the value of count is equal to 100, the LEAVE statement jumps out of the loop.



5. ITERATE statement

The ITERATE statement is also a statement used to break out of a loop. However, the ITERATE statement is to jump out of this loop, and then directly enter the next loop.

The ITERATE statement can only appear within LOOP, REPEAT, and WHILE statements.

The basic grammatical form of the ITERATE statement is as follows:

ITERATE label

Among them, the label parameter represents the sign of the loop.

Below is an example of an ITERATE statement. The code is as follows:
Copy code

add_num: LOOP
SET @count=@count+1;
IF @count=100 THEN
LEAVE add_num ;
ELSE IF MOD(@count,3)=0 THEN
ITERATE add_num;
SELECT * FROM employee ;
END LOOP add_num ;

Copy code

This example loops through the operation of adding 1 to count, and ends the loop when the value of count is 100. If the value of count is divisible by 3, jump out of this loop and no longer execute the following SELECT statement.

Description: Both the LEAVE statement and the ITERATE statement are used to jump out of the loop statement, but the functions of the two are different.

The LEAVE statement is to jump out of the entire loop, and then execute the program after the loop. The ITERATE statement is to jump out of this loop, and then enter the next loop.

Be sure to distinguish clearly when using these two statements.



6. REPEAT statement

A REPEAT statement is a conditional controlled loop statement. When a certain condition is met, it will jump out of the loop statement. The basic syntax of a REPEAT statement is as follows:

[begin_label:] REPEAT
statement_list
UNTIL search_condition
END REPEAT [end_label]

Among them, the statement_list parameter indicates the execution statement of the loop; the search_condition parameter indicates the condition for ending the loop, and the loop ends when the condition is met.

Below is an example of an ITERATE statement. The code is as follows:

REPEAT
SET @count=@count+1;
UNTIL @count=100
END REPEAT ;

This example loops through the operation of adding 1 to count, and ends the loop when the value of count is 100.

REPEAT loops are all terminated with END REPEAT.



7. WHILE statement

WHILE statement is also a conditional control loop statement. But the WHILE statement is not the same as the REPEAT statement.

The WHILE statement is the statement inside the loop that executes when the condition is met.

The basic syntax of a WHILE statement is as follows:

[begin_label:] WHILE search_condition DO
statement_list
END WHILE [end_label]

Among them, the search_condition parameter indicates the condition of the loop execution, and the loop executes when the condition is satisfied; the

statement_list parameter indicates the execution statement of the loop.

Below is an example of an ITERATE statement. The code is as follows:

WHILE @count<100 DO
SET @count=@count+1;
END WHILE ;

This example loops through the operation of adding 1 to count, and executes the loop when the count value is less than 100.

If the count value is equal to 100, then jump out of the loop. The WHILE loop needs to be ended with END WHILE.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326274409&siteId=291194637