初始mysql存储过程

来源:互联网 发布:客观钓鱼岛主权 知乎 编辑:程序博客网 时间:2024/05/29 18:37
 JSON Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

1、什么是存储过程?
    过程就是把若干条sql组合起来并起一个名字即没有返回值的函数(通过其他方式返回)
    把过程存储在数据库中就是存储过程

2、第一个存储过程,创建简单存储过程语法
create procedure procedureName()
begin
--sql
end
    create procedure p1()
    begin
        select concat('AA','BB'); 
    end$
    
3、调用存储过程
call procedureName();

简单存储过程的基本语法
 JSON Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
create procedure procedureName()
    begin
        -- 通过在begin和end之间写sql并批量执行,begin和end是固定的写法
        --通过declare关键字定义变量,下面语句的含义是定义了一个叫age的整型变量默认值是18
        declare age int default 18;
        declare height int default 180;
        select ('年龄是',age,'身高是',height);
    end
    
    create procedure p2()
    begin
        declare age int default 11;
        select concat('年龄是',age);
    end
    
    --set关键字的用法是给定义的变量赋值
    create procedure p3()
    begin
        -- 下面的含义 定义一个叫age的变量,并给其加上10,并在控制台显示出来
        declare age int default 15;
        set age := age+10;
        select concat('10年后的年龄是',age);
    end$
    
    --逻辑判断之if-else分支结构
    create procedure procedureName()
    begin 
        declare age int default 18;
        if age >= 18 then
            select concat('已成年','');
        else
            select concat('未成年','');
        end if;
    end
    
    --逻辑判断之if-elseif-else分支结构,and表示逻辑与 or表示逻辑或
    create procedure p5()
    begin
        declare v int default 49;
        if v >0 and v <= 10 
            then
                select '0<v<10';
        elseif v > 10 and v <= 50
            then 
                select '10 < v <=50';
        else
            select 'v > 50';
        end if;
    end $
    
    --逻辑判断之case-when-then-else分支结构
    create procedure px24()
    begin
        declare randNum int default 0;
        set randNum := floor(rand()*5);
        case randNum
            when 1 then select '1' as result;
            when 2 then select '2' as result;
            when 3 then select '3' as result;
            else
                select 'i dont konw';
        end case;
    end$$

    --循环之while循环结构举例,以while开头 end while结束 
    create procedure px16()
    begin
        declare sum int default 0;
        declare i int default 1;
        -- 每次循环一次对i+1,知道i<=100不满足为止
        while i<=100 do
            set sum := sum+i;
            set i := i+1;
        end while;
        select concat('1+...+100=',sum);
    end$$
    
    --循环之repeat循环结构举例,知道满足util之后条件后结束
    create procedure p11()
    begin
        declare i int default 2;
        repeat
            select concat('i=',i);
            set i := i-1;
        until i <= 0 end repeat ;
    end$

具有参数的存储过程
 JSON Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
--最简单的具有参数输入功能的存储过程举例1
create procedure px15_area(width int,height int)
begin
    select concat('面积是:',width*height) as area;
    if width > height then
        select '你比较胖';
    elseif width < height then
        select '你比较瘦';
    else
        select '你比较方';
    end if;
end$$

--最简单的具有参数输入功能的存储过程举例2
create procedure px17(count int)
begin
    declare sum int default 0;
    declare i int default 1;
    while i <= count do
        set sum := sum + i;
        set i := i+1;
    end while;
    
    select concat('1+...+N=',sum);
end$$

--mysql存储过程类型参数的分类 in/out/inout
--in类型的参数是外界调用存储过程时传入的具体的值(默认就是in类型)
--out类型的参数是存储过程经过计算后把结果返回给这种类型的变量,类似于函数的返回值
--inout类型的参数是具有初始值的变量,存储过程可以改变这个变量
create procedure px18(out age int)
begin
    set age := 0;
    set age := age+10;
end$$
call px18(@age_10)$$

create procedure px19(inout age int)
begin
    set age := age + 30;
end$$
set @age = 19;
call px19(@age)$$
原创粉丝点击