您的位置:首页 > 数据库 > Oracle

oracle 存储过程中使用游标

2013-10-24 10:42 323 查看
项目上准备线,使用存储过程进行基础数据的初始化(有则更新,无则插入)。
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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: