oracle 存储过程中使用游标

项目上准备线,使用存储过程进行基础数据的初始化(有则更新,无则插入)。
1、建立临时表 temp;
2、前台上传文件,写入临时表 temp;
3、通过存储过程,处理temp数据,初始化。

使用游标循环处理临时表数据,进行数据的插入与更新。
begin
  for mycus in cus loop
   begin
    insert table ...
   end;
  end loop;
  for mycus2 in cus2 loop
   begin
    update table ...
   end;
  end loop;
end;

使用上面的方式,即先插入新数据,再更新。会影响游标cus2的结果集,导致数据重复更新。因为游标是在打开的时候,才根据定义,查询结果集。所以insert(update)都会影响后面的结果集。
在不影响初始化数据时,应现在更新操作,在插入新数据。
begin
  for mycus2 in cus2 loop
   begin
    update table ...
   end;
  end loop;
  for mycus in cus loop
   begin
    insert table ...
   end;
  end loop;
end;

猜你喜欢

转载自walrus.iteye.com/blog/1963634