Database flow control statements

1. Statement block, comment and reset command end mark

Statement block

BEGIN

     SQL statement | SQL statement block

END

Description:

① The BEGIN ... END statement block contains all the processing operations of the program block, allowing statement blocks to be nested.

② The use of BEGIN ... END statement block in MySQL alone does not make any sense. It only makes sense to encapsulate it inside stored procedures, stored functions, triggers and other stored programs.

2. Notes

      1) Single line comment

       Use the "##" symbol as a comment for a single-line statement and write it on the line or statement that needs to be commented .

       [Example 3-24] Examples.

        ## Take the maximum of two numbers

       SET @ x = 5, @ y = 6;      ## Define two variables and assign values

       SELECT IF (@x> @ y, @ x, @ y) maximum value; 

      2) Multi-line comments

       Use / * and * / to enclose multiple lines of comment statements.

 3. Reset command end marker

     DELIMITER symbol

   【Description】

   (1) The symbol can be some special symbols, such as two "#", two "@", two "$", and two "%". But avoid using the backslash "/" character, because it is MySQL's escape character.

   (2) To resume using the semicolon as the end marker, execute the DELIMITER; command.

Second, the storage function

1. The creation of stored functions:

      CREATE FUNCTION function name ([parameter name parameter data type [,…]])

      The data type of the value returned by the RETURNS function

      BEGIN

      Function body ;

      RETURN statement;

      END

       Note: with delimiter to characterize the beginning and end

2. Call the stored function

        SELECT function name ([parameter value [,…]])

DELIMITER @@
CREATE FUNCTION name_f1(dno DECIMAL(2))
RETURNS  VARCHAR(14)
BEGIN
  RETURN(SELECT dname FROM dept
    WHERE deptno=dno);  
 END@@

DELIMITER;
SELECT name_f1(20);

3. Delete stored function

      DROP FUNCTION function name;

      Note: Do not add parentheses after the function name.

Three, conditional judgment statement

1. Use of variables in the program

    1) Declare variables

DECLARE local variable name [, local variable name, ...] data type [DEFAULT default value];

    2) Assign values ​​to variables

SET local variable name = expression 1

         [, Local variable name = expression 2, ...];

2. IF statement

     1) Form one

IF  <条件>  THEN

      SQL statement block 1;

[ELSE

      SQL statement block 2;]

END IF;

Supplement: There is a problem here: This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de

The solution to this error when creating a function in MySQL: set global log_bin_trust_function_creators = TRUE;

Example: Determine whether it is a leap year
 

DELIMITER @@  
DROP FUNCTION IF EXISTS leap_year@@  
CREATE FUNCTION leap_year(a int)
RETURNS VARCHAR(255)
 BEGIN 
  IF((a%4=0 AND a%100<>0)OR a%400=0) THEN 
       RETURN '是闰年';
    ELSE 
       RETURN '不是闰年';
    END IF;
 END@@
DELIMITER;
SELECT  leap_year(2001);

2) Form 2

IF <条件1>  THEN

     SQL statement block 1;

ELSEIF  <条件2>  THEN

     SQL statement block 2;

   ……

ELSE

     SQL statement block n;

END IF;

3. CASE statement

1) Form one

CASE <expression>

   WHEN <expression value 1> THEN SQL statement block 1;

   WHEN <expression value 2> THEN SQL statement block 2;

   ……

   WHEN <expression value n> THEN SQL statement block n;

  [ELSE SQL statement block n + 1;]

END;

[Example 3-32] Judging the name and title of the first 3 records in the emp table.

SELECT  ename 姓名,CASE Job
   WHEN '测试' THEN '工程师'
   WHEN '设计'    THEN '设计总监'
   ELSE '经理'
  END AS 职务
 FROM emp  LIMIT 3;


 2) Form 2

 CASE

  WHEN <condition 1> THEN SQL statement block 1;

  WHEN <condition 2> THEN SQL statement block 2;

  ……

  WHEN <condition n> THEN SQL statement block n;

  ELSE SQL statement block n + 1;

END;

[Example 3-33] The judgment displays the name ename, basic salary sal and salary grade of the first three records of the emp table.

SELECT ename,sal,CASE
   WHEN  sal BETWEEN 700 AND 1200 THEN '一级'
   WHEN  sal BETWEEN 1201 AND 1400 THEN '二级'
   WHEN  sal BETWEEN 1401 AND 2000 THEN '三级'
   WHEN  sal BETWEEN 2001 AND 3000 THEN '四级'
   ELSE  '五级'
  END 工资等级
  FROM  emp   LIMIT  3;

Four, loop statement

1. LOOP cycle

 Label: LOOP

  SQL statement block;

  IF <conditional expression> THEN

    LEAVE label;

  END IF;

       END LOOP;

[Example 3-34] LOOP loop statement example. Create a sum_fn () storage function that returns the sum of 1 ~ n.

DELIMITER @@
DROP FUNCTION IF EXISTS sum_fn@@
CREATE FUNCTION sum_fn(n  int)
RETURNS  INT
BEGIN
 DECLARE s,i INT;
 SET s=0,i=1;
 loop_label: LOOP      
   SET s=s+i;
   SET i=i+1;
   IF  i>n  THEN
     LEAVE loop_label; 
   END IF;
 END LOOP;
 RETURN s;
END@@

DELIMITER ;
SELECT sum_fn(5);

2. WHILE cycle

WHILE <conditional expression> DO

    SQL statement block;

END WHILE;

[Example 3-35] WHILE loop statement example. Create a sum_fn () storage function that returns the sum of 1 ~ n.

DELIMITER @@
DROP FUNCTION IF EXISTS sum_fn@@
CREATE FUNCTION sum_fn(n  int)
RETURNS  INT
BEGIN
 DECLARE s,i INT;
 SET s=0,i=1;
 WHILE i<=n DO
  SET s=s+i;
  SET i=i+1;
 END WHILE;
 RETURN s;
END@@
DELIMITER ;
SELECT sum_fn(5);

3. REPEAT cycle

REPEAT

     SQL statement block;

     UNTILE <conditional expression>

END REPEAT;

[Example 3-36] REPEAT loop statement example. Create a sum_fn () storage function that returns the sum of 1 ~ n .

DELIMITER @@
DROP FUNCTION IF EXISTS sum_fn@@
CREATE FUNCTION sum_fn(n  int)
RETURNS  INT
BEGIN
 DECLARE s,i INT;
 SET s=0,i=1;
 REPEAT
  SET s=s+i;
  SET i=i+1;
  UNTIL i>n
 END REPEAT;
 RETURN s;
END@@

DELIMITER ;
SELECT sum_fn(5);

 

Published 75 original articles · praised 164 · 110,000 views

Guess you like

Origin blog.csdn.net/qq_41679818/article/details/105634797