您的位置:首页 > 数据库 > MySQL

mysql使用存储过程返回多个值

2016-08-07 23:16 176 查看
可以使用OUT、INOUT参数类型让存储过程返回多个结果值,存储函数不能胜任,因为只能返回一个值。比如统计student数据表里男生和女生人数并通过它的参数返回这两个计数值,让调用者可以访问它们:

[sql] view
plain copy

delimiter $$  

create procedure count_students_by_sex(out p_male int ,out p_female int)  

begin  

select  count(*) from student where sex= 'M' into p_male;  

select count(*) from student where sex='F' into p_feamle;  

end $$  

delimiter ;  

 

在调用这个过程的时候,把参数替换为用户自定义变量。如:

[sql] view
plain copy

CALL count_students_by_sex(@mcount,@fcount);  

select 'Number of male students:',@mcount;  

 

结果:

Number of male studens:@mcount
Number of students:16
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  存储过程 in out