1、创建一个存储过程,在数据库d_eams的student 表中查询"政治面貌"为"共青团员"的学生的学号、姓名、性别及政治面貌信息
use d_eams;
delimiter $$
create procedure cp_student(in zzmm char(8))
begin
select 学号, 姓名, 性别, 政治面貌
from student
where 政治面貌=zzmm
order by 学号;
end $$
delimiter ;

2、调用存储过程
call cp_student('共青团员');

3、在d_eams数据库中的student表中创建一个存储过程,通过建立一个性别参数为同一存储过程指定不同的性别,用于返回不同性别的学生信息
use d_eams;
delimiter @@
create procedure cp_sex(in xb char(2))
begin
select * from student
where 性别=xb;
end@@
delimiter ;

4、执行带有输入参数的存储过程cp_sex
call cp_sex('女');

5、创建一个名为cp_score的存储过程。他使用两个参数:"p_姓名"为输入参数,用于指定要查询的学生姓名;"p_成绩" 为输入参数,用来返回学生的成绩
use d_eams;
delimiter $$
create procedure cp_score(in p_name char(10), out p_score decimal(4,1))
begin
select b.成绩 into p_score from student a, sc b
where a.学号=b.学号
and 姓名=p_name;
end$$
delimiter ;

6、返回参数值
call cp_score('高峰',@p_score);
select concat('高峰', '的成绩是:', @p_score) as 结果;

7、修改存储过程"cp_student"的定义,具有写数据的权限,并且调用者可以执行
alter procedure cp_student
modifies sql data
sql security invoker;

8、删除cp_student存储过程
drop procedure if exists cp_student;

9、查看存储过程
查看cp_score 存储过程的定义文本信息
show procedure status like 'cp_score';

查看cp_score存储过程的详细文本信息
show create procedure cp_score;

10、存储过程中的异常处理
在d_eams 数据库中创建一个存储过程p_insert, 向student 表中插入一条记录('201007001','张静', '女', '2000-2-1', '汉族', '共青团员');已知学号201007001已存在于student表中
use d_eams;
delimiter $$
create procedure p_insert()
begin
declare info int default 0;
declare continue handler for sqlstate '23000' set @info = 1;
insert into student
values('201007001','张静','女','2000-2-1','汉族','共青团员');
end $$
delimiter ;

调用查看数据结果
call p_insert();
select @info;

11、创建查询选课记录的存储过程
(1)创建一个存储过程P_score ,在d_eams数据库的T_sc 成绩表中查询成绩为60分以上的学生学号、课程号、成绩信息。
use d_eams;
delimiter $$
create procedure P_score()
begin
select 学号, 课程号, 成绩
from sc
where 成绩>=60
order by 学号;
end$$
delimiter ;

执行P_score1
call P_score();

(2)创建一个名为"P_score1"的存储过程。输入学生姓名,返回学生成绩
use d_eams;
delimiter $$
create procedure P_score1(in p_name1 char(10), out p_score1 decimal(4,1))
begin
select b.成绩 into p_score1 from student a, sc b
where a.学号=b.学号
and 姓名=p_name1;
end $$
delimiter ;

以上代码为了接收一个存储过程的返回值,需要一个变量来存放返回的参数的值
call P_score1('刘海燕', @p_score1);
select concat('刘海燕', '的成绩是:', @p_score1) as 结果;