9.存储过程

存储过程procedure,类似于编程中的函数。先预编译一块很少sql语句的集合。定义形参,
使用的时候通过传参数来调用。

create procedure 存储过程名(形参)
begin
sql语句
sql语句
end;

例: 写一个存储过程,可以查询所有男生的信息,再查询所有系主任的信息


create procedure pro_a()
begin
select * from t_student where ssex='男';
select * from t_man;
end;


mysql控制台默认是’;‘是结尾符号,如果需要写存储过程则需要改结束符号。
delimiter #

调用存储过程 call 存储过程名();

根据存储过程的参数
带参数的存储过程
参数分3类:
IN:表示存储过程需要给它一个值 (默认)
OUT:表示存储过程会返回一个值
INOUT:既可以传值进去也可以带值出来
例:
传入一个学生的学号,输出这个学生的信息
create procedure pro_b(in a int)
begin
select * from t_student where sid = a;
end;

call pro_b(1202)

传入一个班级编号和系编号,输出这个班有多少人
create procedure pro_c(in a int,in b int)
begin

select count(*) from t_student where sclass=a and did = b;

end


例: 写一个存储过程,返回1202的年龄
create procedure pro_d(out x int)
begin
select sage into x from t_student where sid = 1202;
end

如果使用out类型,则在调用之前需要先定义一个变量
语法: set @x;
call pro_d(@x)
select @x;

例: 传入一个系编号,返回这个系有多少人。
create procedure pro_e(in a int,out b int)
begin

select count(*) into b from t_student where did = a;

end

例: 一个存储过程,通过sid给这个学生的分数重新赋值.
create procedure pro_m(in a int ,in b int)
begin

update t_student set sscore = b where sid = a;

end


inout:
传入一个学号,传出这个学生的年龄
create procedure pro_n (inout x int)
begin
select sage into x from t_student where sid = x;
end


create procedure pro_x(in a int,in b int)
begin
select * from t_student limit a,b;
end;




//逻辑 交给java

原文地址:https://www.cnblogs.com/makalochen/p/10656597.html