No_data_found存在资源数据时出错

时间:2014-05-04 22:28:34

标签: oracle10g

我对PL / SQL比较陌生,所以我要问的问题可能听起来有点基本。我正在尝试运行以下代码并在不应该的时候收到No_Date_Found错误。我正在尝试执行循环。我正在尝试根据另一个变量为变量选择一组值。突出显示的粗线是发生错误的位置。如果我用该行中的文字值替换v_tariff_code变量,则不再发生错误。请帮忙吗?

set serveroutput on
declare
v_remaining_arrears number := 0;
v_tariff_code date;
v_tariff_store date; --a variable that stores the value of the previous tariff while the above variable takes the next tariff code value 
v_session_start_date date; --session start date
v_con_date date; --connection date
v_start_date date;
v_c_code varchar2(10);
v_cat_code number;
vunits number;
v_w_rate number;
v_s_rate number;
v_multiples number;
v_session_current varchar2(20);
v_counter number;
v_date1 date;
v_date2 date;


begin
v_c_code := 'D2203447'; 
v_session_current := 'JAN2014-FEB2014';
--V_SESSION_START_DATE := '01-JAN-14';
--v_date1 := v_session_start_date; --v_date1 would always begin as the session start date
--select v_session_start_date into v_date1 from dual;
--loop
select cat_code, units, con_date into v_cat_code, vunits, v_con_date from consumer where c_code = v_c_code;
    select start_date into v_session_start_date from bill_session where session_code = v_session_current;


v_start_date := v_session_start_date;

loop
v_start_date := v_start_date - 30;

select MAX(tariff_code) INTO v_TARIFF_CODE from tariff where tariff_code < V_START_DATE;

**select w_rate, s_rate into v_w_rate, v_s_rate from cat_tariff where cat_code = v_cat_code and 
tariff_code = v_tariff_code;** 



v_remaining_arrears := v_remaining_arrears + ((round(v_w_rate *v_multiples*vunits) + round(v_s_rate * vunits)));

exit when v_start_date < v_con_date;


--DBMS_OUTPUT.PUT_LINE('ARREARS = ' || V_REMAINING_ARREARS);

end loop;

DBMS_OUTPUT.PUT_LINE('ARREARS = ' || V_REMAINING_ARREARS);

end;

你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

select w_rate, s_rate 
into v_w_rate, v_s_rate 
from cat_tariff 
where cat_code = v_cat_code and tariff_code = v_tariff_code;

这是一个精确的提取,必须返回一个记录。查询返回零或更多,然后一个记录导致异常。

尝试在查询之前添加dbms_output.put_line(v_cat_code || ',' || v_tariff_code);。在此异常之前,此行会显示v_cat_codev_tariff_code的问题值。

可能你想要忽略这个异常,只处理明确返回数据的情况。在这种情况下,您应使用BEGIN … EXCEPTION … END

begin
  select w_rate, s_rate 
  into v_w_rate, v_s_rate 
  from cat_tariff 
  where cat_code = v_cat_code and tariff_code = v_tariff_code;
  -- DO SOMETHING USEFULL WITH v_w_rate, v_s_rate 
exception
  when no_data_found then null;
  when others then raise;
end;