MySQL 存储过程的使用

  1. 查询存储过程
show procedure status where db='数据库名';
  1. 删除存储过程(test为存储过程名称)
drop procedure if exists test;
  1. 创建存储过程和调用(里面涉及到循坏和游标)
delimiter //  # 定义//为一句sql的结束标志,取消;的所表明的意义
drop procedure if exists test;  # 若是存在名字为test的procedure则删除
create procedure test()  # 建立(建立函数使用的关键字为function 函数名())
begin
    declare score int;  # 声明变量
    declare temp_id int;
    declare flag int default 0;
    # 这是重点,定义一个游标来记录sql查询的结果(此处的知识点还有SQL的模糊查询,见补充)
    declare s_list cursor for select id, login_score + course_study_score + live_study_score + book_study_score + interactive_score + homework_score +station_score from stu_grade_record; 
    # 为下面while循环创建一个退出标志,当游标遍历完后将flag的值设置为1
    declare continue handler for not found set flag=1;
    open s_list;  # 打开游标
    # 将游标中的值赋给定义好的变量,实现for循环的要点
        fetch s_list into temp_id, score;
        while flag <> 1 do
            # 在T-SQL中,局部变量必须以@做为前缀
            #SELECT temp_id,score; //select 可以用作打印
            # 根据id的惟一性,利用当前查询到的记录中的字段值来实现更新
            update stu_grade_record set usually_score=score where id=temp_id;
            # 游标日后移(此处的游标是否是让你想起了C里面的指针)
            fetch s_list into temp_id, score;
        end while;
        #select * from temp_table;
    close s_list;  # 关闭游标
end
//
delimiter ;  # 从新定义;为一句sql的结束标志,取消//的所表明的意义
call test(); # 调用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值