目录
创建存储过程
Create procedure 存储过程名
Begin (开始)
…………
…………
End(结束)
调用存储过程
Call 存储过程名(in 变量名 数据类型,out 变量名 数类型 );
删除存储过程
Drop procedure 存储过程名;
修改SQL语句结束符
Delimiter 结束符
1.创建一个stu表
执行 结果如图:
代码如下:
create table stud(
-> id int(10) primary key not null unique,
-> name varchar(50) not null,
-> class varchar(50) not null)/
insert into stud values
-> ("1","lucy","class1"),
-> ("2","tom","class2"),
-> ("3","rose","class3")/
2. 创建一个存储过程
执行结果如图:
代码如下所示:
create procedure addcount (out count int)
-> begin
-> declare itmp int;
-> declare cur_id cursor for select id from stud;
-> declare exit handler for not found close cur_id;
-> select count( * ) into count from stud;
-> set @sum=0;
-> open cur_id;
-> repeat
-> fetch cur_id into itmp;
-> if itmp<10
-> then set @sum=@sum+itmp;
-> end if;
-> until 0 end repeat;
-> close cur_id;
-> end/
call addcount(@count)/
select@count,@sum/