zoukankan      html  css  js  c++  java
  • MySQL知识总结(三)存储过程

    1. 创建存储过程

    1.1 无参数存储过程

    CREATE PROCEDURE bruce_procedure ()
    
    BEGIN
    
    --1.声明变量
    
    --2.执行业务逻辑
    
    END

    1.2 有参数的存储过程

    CREATE PROCEDURE bruce_procedure (
    
    IN in_exam_id INT,
    
    in_student_types_id VARCHAR (100)
    
    )
    
    BEGIN
    
    --1.声明变量
    
    --2.执行业务逻辑
    
    END

    IN 表示输入参数

    2. 调用存储过程

    --无参调用
    
    Call bruce_procedure();
    
    --有参调用
    
    Call bruce_procedure(1,’2,1’);

    3. 删除存储过程

    Drop procedure bruce_procedure;

    4. 查看存储过程信息

    1) show procedure status

    显示数据库中所有存储的存储过程基本信息,包括所属数据库,存储过程名称,创建时间等

    2) show create procedure sp_name

    显示某一个存储过程的详细信息

    5. 声明变量

    CREATE PROCEDURE sc_st_group_courses_class_procedure (
    
    IN in_exam_id INT,
    
    in_student_types_id VARCHAR (100)
    
    )
    
    BEGIN
    
    -- 组织课程组关系id
    
    DECLARE v_group_courses_id INT;
    
    -- 考试组织id
    
    DECLARE v_exam_group_id INT;
    
    END

    1) 可以在存储过程全局范围内声明

    2) 如果要在局部范围内声明,需重新定义begin end,并在begin和end之间声明变量

    6. If

    if stop = 1 then
    
    leave cur_loop;
    
    end if;

    7. 循环

    7.1 结构

    cur_loop : loop
    
    //业务逻辑
    
    end loop //结束循环

    7.2 跳出循环

    leave cur_loop;

    8. 游标

    8.1 声明游标

    CREATE PROCEDURE sc_st_group_courses_class_procedure() 
    
    BEGIN DECLARE STOP INT DEFAULT 0;
    
    -- 创建游标
    
    DECLARE cur CURSOR FOR (
    
    select
    
    group_courses_id,
    
    exam_group_id,
    
    courses_id,
    
    class_id
    
    from sc_group_courses_view WHERE group_class='class' and exam_id = in_exam_id
    
    );
    
    -- 游标捕捉越界异常
    
    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET STOP = 1;
    
    END

    8.2 打开和关闭游标

    打开游标:OPEN cur

    关闭游标:CLOSE cur

    8.3 遍历游标

    CREATE PROCEDURE sc_st_group_courses_class_procedure()
    
    BEGIN DECLARE STOP INT DEFAULT 0;
    
    -- 创建游标
    
    DECLARE cur CURSOR FOR (
    
    select
    
    group_courses_id,
    
    exam_group_id,
    
    courses_id,
    
    class_id
    
    from sc_group_courses_view WHERE group_class='class' and exam_id = in_exam_id
    
    );
    
    -- 游标捕捉越界异常
    
    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET STOP = 1;
    
    -- 开启游标
    
    OPEN cur;
    
    -- 开始循环
    
    cur_loop : loop
    
    FETCH cur INTO
    
    v_group_courses_id,
    
    v_exam_group_id,
    
    v_courses_id,
    
    v_class_id
    
    ;
    
    -- 判断游标溢出
    
    if stop = 1 then
    
    leave cur_loop;
    
    end if;
    
    -- 结束循环
    
    END loop cur_loop;
    
    -- 关闭游标
    
    CLOSE cur;
    
    END

    9. 给变量赋值

    DECLARE v_median_index int;

    set v_median_index = v_class_count/2-1;

    10. 查询并赋值给变量

    select
    
    max(sum_score),min(sum_score),avg(sum_score),STD(sum_score)
    
    into
    
    v_highest_score,
    
    v_lowest_score,
    
    v_average,
    
    v_std
    
    from a
    
    ;

    11. 参考

    http://www.ccvita.com/100.html

  • 相关阅读:
    第十二章学习笔记
    UVa OJ 107 The Cat in the Hat (戴帽子的猫)
    UVa OJ 123 Searching Quickly (快速查找)
    UVa OJ 119 Greedy Gift Givers (贪婪的送礼者)
    UVa OJ 113 Power of Cryptography (密文的乘方)
    UVa OJ 112 Tree Summing (树的求和)
    UVa OJ 641 Do the Untwist (解密工作)
    UVa OJ 105 The Skyline Problem (地平线问题)
    UVa OJ 100 The 3n + 1 problem (3n + 1问题)
    UVa OJ 121 Pipe Fitters (装管子)
  • 原文地址:https://www.cnblogs.com/tangyanbo/p/4289998.html
Copyright © 2011-2022 走看看