MySQL存储过程
存储过程
1.创建存储过程,接收账户id参数查询该账户余额。(in参数类型示例)
delimiter $$ #自定义结束符号
create proceduer sp_in_balance(balance_id int)
begin
select accname,balance from account where id=balance_id;
end
$$
#调用存储过程
call sp_in_balance(id) $$
2.创建存储过程,接收账户id参数,并将查询到的余额存储到输出参数中(out参数类型示例)
delimiter $$ #自定义结束符
create procedure sp_out_balance(balance_id int,out money double)
begin
select balance into money from account where id=balance_id;
end
$$
#调用存储过程
call sp_out_balance(1,@x) $$
3.创建存储过程,查询account表中的记录总数
delimiter $$
create procedure sp_count(inout total int)
begin
if (total is not null)then
select count(*) into totalfrom account;
end if;
end
$$
#调用存储过程
set @x = 1 $$
call sp_count(@x) $$
select @x $$