#【例10.4】创建存储过程P_math,如果”高等数学”课程的平均成绩大于80分,则显示”高等数学成绩良好”,否则显示”高等数学成绩一般”。
use stusys;
select * from course a,score b
where a.cno=b.cno;
delimiter //
create procedure P_course(in v_cname char(10),out v_grade char(20))
begin
#定义或者声明一个精准类型的变量,变量结果为4给字符,2位小数点
declare v_avg decimal(4,2);
#将列表赋值给变量
select avg(b.grade) into v_avg
from course a,score b
where a.cno=b.cno and a.cname=v_cname;
#流程控制语句:选择结构if...then...else...end if;
if v_avg>80 then
#将单个数据赋值给变量
set v_grade='成绩良好';
else
set v_grade='成绩一般';
end if;
end //
delimiter ;
#调用存储过程时,要定义用户变量前面加@符号
call P_course('数据库系统',@grade);
select @grade