mysql> 1.创建存储过程,实现从m加到n,显示其和.
mysql> create procedure sp_addmn(m int,n int)
-> begin
-> declare s int;
-> set s=0;
-> while m<=n
-> do begin
-> set s=m+s;
-> set m=m+1;
-> end;
-> end while;
-> select s as 和;
-> end#
Query OK, 0 rows affected (0.00 sec)
-> 2.按照等级进行加分,如果等级为优秀,将其分值按照10%进行提分,最高不超过98分;如果等级为不及格,将其分值按10%进行减分,最低不超过40分.
mysql> create procedure aa(name char(8),cn char(8),g char(8))
-> begin
-> declare score real;
-> set score=(select 成绩 from v_sc where 学生姓名=name and 课程名=cn);
-> if score>=80 then set g='优秀';
-> elseif score>=70 and score<80 then set g='良好';
-> elseif score>=60 and score<70 then set g='及格';
-> else set g='不及格';
-> end if;
-> case
-> when g='优秀' and score*1.1<98 then update v_sc set 成绩=成绩*1.1 where
成绩>=80;
-> when g='良好' or g='及格' or g='不及格' and score*0.9>40 then
-> update v_sc set 成绩=成绩*0.9;
-> end case;
-> select 学生姓名,课程名,成绩,g as 等级 from v_sc
-> where 学生姓名=name and 课程名=cn;
-> end#
Query OK, 0 rows affected (0.00 sec)
-> 3.创建存储过程实现多次加减分.
mysql> create procedure aa(name char(8),cn char(8),g char(8),i int)
-> begin
-> declare score real;
-> declare j int;
-> set j=1;
-> set score=(select 成绩 from v_sc where 学生姓名=name and 课程名=cn);
-> if score>=80 then set g='优秀';
-> elseif score>=70 and score<80 then set g='良好';
-> elseif score>=60 and score<70 then set g='及格';
-> else set g='不及格';
-> end if;
-> while j<=i
-> do begin
-> case
-> when g='优秀' and score*1.1<98 then update v_sc set 成绩=成绩*1.1 where
成绩>=80;
-> when g='良好' or g='及格' or g='不及格' and score*0.9>40 then
-> update v_sc set 成绩=成绩*0.9;
-> set j=j+1;
-> end case;
-> end;
-> end while;
-> select 学生姓名,课程名,成绩,g as 等级 from v_sc
-> where 学生姓名=name and 课程名=cn;
-> end#