mysql创建含参存储过程及其使用,循环语句repeat until使用
创建语句
create database db1;
use db1;
create table grade (
sno char(4),
cno char(4),
num int
);
insert into grade value('001','001',90),('001','002',80),('002','001',87),('002','002',98),('003','001',60),('003','002',80);
//创建含参存储过程sum1求任意一个学生的总成绩
delimiter &
create procedure sum1(in num1 char(4),out sum1 int)
begin
select sum(num) into sum1 from grade where sno=num1;
end&
delimiter ;
//创建含参存储过程sum2循环求1到10的和
delimiter &
create procedure sum2(in num2 int)
begin
declare n int default 1;
declare sum2 int default 0;
repeat
set sum2 = sum2+n;
set n = n+1;
until n>num2
end repeat;
select sum2;
end&
delimiter ;
运行语句
use db1;
call sum1('001',@sum1);
select @sum1 as 总分;
call sum2(10);
结果
注意
- 在执行mysql存储过程: call sum1(); 时,报错: [Err] 1318 - Incorrect number of arguments for PROCEDURE DB.pro_one;
expected 1, got 0
大意: 参数不正确。
解决:添加参数时缺少了输出参数@sum1 导致参数不匹配。
- 在执行mysql循环过程:Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘; end repeat; select sum2; end’ at line 8
解决:until 后不要添加分号。