Study Notes (10): mySQL database development tutorial - using variables and determine the statements in stored function

Learning immediately: https://edu.csdn.net/course/play/4364/77148?utm_source=blogtoedu

mySQL, the variable is determined and stored in the cyclic procedure can be used and stored functions.

# 创建使用IF的函数,IF可以嵌套,END IF要成对
create function fab(a int, b int)
returns varchar(20)
begin
declare result varchar(20);
IF a>b then set result='a大于b';
ELSE IF a=b then set result='a等于b'; 
        ELSE set result='a小于b';
        end if;  -------------------------- end嵌套的else if语句
end IF;
RETURN result;
end
# 创建使用case的函数,多分支判断
create function fab2(a int, b int)
returns varchar(20)
begin
declare result varchar(20);
case when a>b then set result='a大于b';
        when a=b then set result='a等于b';
        else set result='a小于b';
end case;
return result;
end

# 判断一个整数除以4后余数
create function fb(sint int)
returns varchar(20)
begin
declare result varchar(20);
case sint%4 when 0 then set result='余0';
                   when 1 then set result='余1';
                   when 2 then set result='余2';
                   else set result='余3';
                   end case;
return result;
end

 

Published 15 original articles · won praise 0 · Views 87

Guess you like

Origin blog.csdn.net/weiying_zh/article/details/105281358