oracle教程:条件判断

declare
 v_price1 number(10,2);--单价
 v_price2 number(10,2);--单价
 v_price3 number(10,2);--单价
 v_usenum2 number(10,2);--吨数
 v_money number(10,2);--金额
 v_account t_account%rowtype;--台帐行记录类型
begin
  v_price1:=2.45;--单价赋值(5吨以下)
  v_price2:=3.45;--单价赋值(5-10吨)
  v_price3:=4.45;--单价赋值(超过10吨)
 select * into v_account from t_account
 where year='2012' and month='01' and ownerid=1;
  v_usenum2:=round(v_account.usenum/1000,2);--吨数
  --v_money:=v_usenum2*v_price;--金额
  --阶梯水费计算
  if v_usenum2<=5 then
    v_money:=v_price1*v_usenum2;
  elsif v_usenum2>5 and v_usenum2<=10 then
    v_money:=v_price1*5+v_price2*(v_usenum2-5);
  else
    v_money:=v_price1*5+v_price2*5 + v_price3*(v_usenum2-10);
  end if;
  
  dbms_output.put_line('水费字数:'||v_account.usenum||'金额:'||v_money);
  exception
    when no_data_found then
      DBMS_OUTPUT.put_line('没有数据');
    when too_many_rows then
      DBMS_OUTPUT.put_line('返回多行');
end;

猜你喜欢

转载自blog.csdn.net/a772304419/article/details/132477807