declare
v_rate NUMBER;
v_threshould NUMBER := 100.00;
v_increase_flag BOOLEAN := false;
begin
select rate into v_rate from rate_table
where rate_type = 'Monthly'
and effective_date = trunc(sysdate);
if(v_rate < v_threshould) then
v_increase_flag := true;
end if;
if(v_inscrease_flag) then
update rate_table set rate := rate + 2.5 * rate
where rate_type = 'Monthly'
and effective_date = trunc(sysdate);
commit;
end if;
exception when no_data_found then
dbms_output.put_line('Rate is not available');
when others then
dbms_output.put_line('Server error');
dbms_output.put_line(sqlerrm);
end;
/
上面为一个PL/SQL最基本的程序结构,这个PL/SQL块同时可以嵌入到存储过程中
create or replcae procedure p_process_rate(
ip_rate_type in rate_table.rate_type%type,
ip_effective_date in rate_table.effective_date%type,
ip_threshould in number,
op_rate out number,
op_success out varchar2,
op_errmsg out varchar2)
is
v_rate number;
v_increase_flag boolean := false;
begin
select rate into v_rate from rate_table
where rate_type = ip_rate_type
and effective_date = ip_effective_date;
if(v_rate < ip_threshould) then
v_increase_flag := true;
end if;
if(v_increase_flag) then
update rate_table set rate := rate + 2.5 * rate
where rate_type = ip_rate_type
and effective_date = ip_effective_date
returning rate into v_rate;
commit;
end if;
op_rate := v_rate;
op_success := 'Y';
exception when no_data_found then
op_rate := null;
op_success := 'N';
op_errmsg := 'Rate is not availabe ';
when others then
op_rate := null;
op_success := 'N';
op_errmsg := 'Server error' || chr(10) || chr(13) || SQLERRM;
end;
/
其中in表示输入参数,out表示输出参数
declare
v_rate number;
v_success_flag varchar2(1);
v_errmsg varchar2(1000);
begin
p_process_rate('Monthly',trunc(sysdate), 100.00,
v_rate, v_success_flag, v_errmsg);
if(v_success_flag != 'Y') then
dbms_output.put_line('Procedure p_process_rate failed with error(s) as follows');
dbms_output.put_line(v_errmsg);
end if;
end;
/