create or replace procedure sp_trans_money
(
p_from number,
p_to number,
p_money NUMBER
)
is
v_count number;
v_lock number;
v_money number;
begin
--to是否存在
select count(*) into v_count from t_account where u_id = p_to;
if v_count = 0 then
raise_application_error(-20087, '目标用户不存在!');
end if;
--to是否锁定
select u_lock into v_lock from t_account where u_id = p_to;
if v_lock = 1 then
raise_application_error(-20088, '该用户已锁定!');
end if;
--from是否存在
select count(*) into v_count from t_account where u_id = p_from;
if v_count = 0 then
raise_application_error(-20087, '转账用户不存在!');
end if;
--from是否锁定及余额
select u_lock, u_money into v_lock, v_money
from t_account
where u_id = p_from;
if v_lock = 1 then
raise_application_error(-20088, '该用户已锁定!');
elsif (v_money - p_money < 10) then
raise_application_error(-20089, '该用户余额不足!');
end if;
update t_account set u_money = u_money - p_money where u_id = p_from;
update t_account set u_money = u_money + p_money where u_id = p_to;
--通常是由存储过程提交事务
--如果存储过程不提交,则由调用方提交事务
--好处:可以将多个存储过程放在一个事务中,但通常一个存储过程就是处理一个单独的事务,所以在实际开发中,具体情况具体处理
--commit;
end;