MYSQL中如何调用带输出参数的存储过程
不同于带入参的过程,参数不能常量输入,可以(随便)放一个用户变量@outp调用过程,执行完毕后再(就像使用函数那样)查看该变量值就可以了。
mysql> create procedure add_id(out count int)
-> begin
-> declare itmp int;
-> declare cur_id cursor for select id from sch;
-> declare exit handler for not found close cur_id;
-> select count_sch() into count;
-> 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 //
mysql> call add_id(@test) //
Query OK, 0 rows affected (0.00 sec)
mysql> select @test,@sum //
+-------+------+
| @test | @sum |
+-------+------+
| 2 | 3 |
+-------+------+
1 row in set (0.00 sec)