日期:2014-05-17  浏览次数:20696 次

存储过程老是出错?
C# code

declare   
  cid   number(20); 
begin
   select a.id into cid   from b_table a where a.mac='911:34:34:34:GG:GG';
   if cid >0
     then
        update 语句;
   else
      insert 语句;
     end if;
 commit; 
exception
when others then
rollback;
end;



有一个问题,当where条件为真的时候,正确;但是当where条件为假,else就不会走,这是什么情况啊?

------解决方案--------------------
当where条件找不到值的时候,就发生了NO_DATA_FOUND异常.直接rollback了.
解法:select 语句需要改成count计数方式
SQL code
declare   
  cid   number(20); 
begin
   select count(a.id) into cid   from b_table a where a.mac='911:34:34:34:GG:GG';
   if cid >0
     then
        select a.id into cid   from b_table a where a.mac='911:34:34:34:GG:GG';
        update 语句;
   else
      insert 语句;
     end if;
 commit; 
exception
when others then
rollback;
end;

------解决方案--------------------
把declare去掉。