1. 程式人生 > >mysql使用儲存過程返回多個值

mysql使用儲存過程返回多個值

可以使用OUT、INOUT引數型別讓儲存過程返回多個結果值,儲存函式不能勝任,因為只能返回一個值。比如統計student資料表裡男生和女生人數並通過它的引數返回這兩個計數值,讓呼叫者可以訪問它們:

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 ;

在呼叫這個過程的時候,把引數替換為使用者自定義變數。如:

CALL count_students_by_sex(@mcount,@fcount);
select 'Number of male students:',@mcount;

結果:

Number of male studens: @mcount
Number of students: 16