存储过程

会有一股神秘感。 提交于 2020-02-26 14:16:08
create procedure 存储过程名(参数)
begin
     sql语句;
end;

修改数据库结束符号的sql语句,将;修改成其他符号:比如“#”

delimiter #

修改数据库的结束符号,则存储过程sql语句修改如下:

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

调用执行存储过程:

call 存储名(参数)#

带参数的存储过程参数类型:

in: 将参数带进去
out:将参数带出来
inout: 可以带进去又可以带出来
create procedure pro_c( in x int)
begin
     select * from t_student where id = x;
end#



调用
call pro_c(12) #

多个参数创建存储过程

create procedure pro_e( in sclass int , in enname varchar(20))
begin
     select * from t_student where class = sclass or name = enname;
end # 

OUT类型存储过程

存储过程:获取3班成绩最高的存储过程
create procedure pro_r1(out x int)
begin
  select max(score) into x from t_student where sclass = 3;
end #

先定义x的值(mysql中定义变量必须加@符号)

set @x = 1#

调用存储过程

call pro_r1(@x) #

获取最终最高成绩的值

select @x#

 INOUT类型存储过程

create procedure pro_r2(in x int, out name varchar(20))
begin
  select sname into name from t_dept where did = x;
end #
  

准备变量

set @x = 'abc' #

调用存储过程

call pro_r2(1,@x) #

获取返回值

select @x #

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!