【MySQL复习】存储过程

存储过程是一组为了完成特定功能的sql语句集合,存储在数据库中的,用户通过指定的存储过程名和参数进行调用。

存储过程相当于是定义在MySQL中的方法,开发者可以直接调用。

参数:输入输出参数 参数名称 数据类型参数

入参:相当于Java方法的中输入参数

出参:相当于Java中的返回值

创建存储过程:

create procedure 名字(in target int)
begin
    declare name varchar(11);
       if target = 1 then 
        set name = 'mysql';
        else 
        set name = 'java';
        end if ;
     insert into tableName(name) values(name);
end;

调用存储过程:

call 名字;

删除存储过程:

drop procedure 名字;

出参:

create procedure count_num(out num int)
  begin
    select count(score) into num from student;
end;

调用:

call procedure count_num(@countNum);
select @countNum;

 

posted @ 2022-02-28 14:53  小李不背锅  阅读(34)  评论(0编辑  收藏  举报