MySQL 循环分支语法

---恢复内容开始---

Loop循环

label: LOOP
    statement_list

    IF exit_condition THEN
        LEAVE label; 
    END IF; 
END LOOP label;
View Code

 

 

Repeat循环

REPEAT
    statement_list
UNTIL search_condition END REPEAT;
View Code

 

 

While循环

WHILE search_condition DO
    statement_list
END WHILE;
View Code

 

 

IF...ELSE分支判断

IF search_condition THEN
    statement_list
ELSE
    statement_list
END IF;
View Code

 

 

IF...ELSEIF...ELSE分支判断

IF search_condition THEN
    statement_list
ELSEIF search_condition THEN
    statement_list
ELSE
    statement_list
END IF;
View Code

 

 

CAST分支判断

CASE case_value
    WHEN when_value THEN
        statement_list
    ELSE
        statement_list
END CASE;
View Code

 

 

创建存储过程(PROCEDURE)

create PROCEDURE procedure_name([IN|OUT|INOUT] param_name param_type,...)
BEGIN
    statement_list
END
View Code

 

 

删除存储过程(PROCEDURE)

DROP PROCEDURE procedure_name
View Code

 

 

查看存储过程(PROCEDURE)

SELECT
    * 
FROM
    mysql.proc 
WHERE
    type = 'PROCEDURE'
View Code

 

 

创建函数(FUNCTION)

CREATE FUNCTION function_name(param_name param_type,...)
RETURNS return_type
BEGIN
    statement_list
END
View Code

 

 

删除函数(FUNCTION)

DROP FUNCTION IF EXISTS function_name
View Code

 

 

查看函数(FUNCTION)

SELECT
    * 
FROM
    mysql.proc 
WHERE
    type = 'FUNCTION'
View Code

 

定义变量赋值

-- 函数(FUNCTION)或存储过程(PROCEDURE)定义变量及赋值
    -- 定义
    DECLARE variable_name variable_type DEFAULT default_value;
    -- 赋值
    SET variable_name = new_value;
    -- 在SELECT查询里进行赋值
    SELECT variable_name := col_name;
View Code

 

关于定义变量赋值的详细说明:https://www.cnblogs.com/EasonJim/p/7966918.html

关于动态MySQL语句:http://blog.itpub.net/29773961/viewspace-1852824/

posted @ 2018-09-05 21:39  星小梦  阅读(195)  评论(0编辑  收藏  举报