语法结构:
CREATE FUNCTION function_name([param type ... ]) RETURNS type BEGIN ... END;
案例 :
定义一个存储函数, 请求满足条件的总记录数 ;
delimiter $
create function count_city(countryId int)
returns int
begin
declare cnum int ;
select count(*) into cnum from city where country_id = countryId;
return cnum;
end$
delimiter ;
调用:
select count_city(1);
select count_city(2);