1. 程式人生 > >oracle 捕獲select into異常

oracle 捕獲select into異常

SELECT INTO 要求有一條返回資料,所以沒有返回(NO_DATA_FOUND)或者返回多於一條(TOO_MANY_ROWS)都會丟擲異常;
 select into 語句嵌入begin ... exception ... end中處理
 
 begin    
      select e.employeecode,e.employeename 
      into v_code,v_name
      from employee e, empgroup eg 
      where e.employeeid = eg.employeeid and c1.orgid = eg.orgid 
      and eg.empgroupid = 100000204;
 exception 
 when no_data_found then 
   v_code:=null;
   v_name:=null;
 when others then
   v_code:=null;
   v_name:='存在相同條件的多條記錄啦!';
 end;
 
begin    
      select e.employeecode,e.employeename 
      into v_code,v_name
      from employee e, empgroup eg 
      where e.employeeid = eg.employeeid and c1.orgid = eg.orgid 
      and eg.empgroupid = 100000204 and rownum = 1;
 exception 
 when others then 
   v_code:=null;
   v_name:=null;
 end;