求教!游标中不fetch下一条记录

[复制链接]
查看11 | 回复6 | 2016-1-6 14:01:09 | 显示全部楼层 |阅读模式
本帖最后由 cz6024 于 2012-11-6 13:13 编辑
v_type
------外部已经定义的变量
v_money
------外部已经定义的变量
v_amount
------外部已经定义的变量
declare

cursor mycursor is select feiyongleixing,huopinleibie from scm_tab d --------定义游标
begin
open mycursor;

loop

fetch mycursor into v_bl1,vbl2 ;(v_bl1,vbl2外部已经定义的变量)

exit when mycursor%notfound;

selectt.priceinto v_price ------ (v_price外部已经定义好的变量)

from cp_tab twhere t.column1 = v_bl1and t.column2=v_bl2 ;---------根据fetch的记录,查找 price

v_money:= v_price *v_amount ;
------查找出来的price 参与计算

ifv_type = 1 then

begin

insert into table3 ( v_money)--------结算结果插入到表中

values ();

end;

end if;

end loop;

close mycursor;
end;
发现的问题 select 部分查找price时,如果找不到记录,游标不fetch下一记录了,直接跳出loop了
找不到price时,如何让游标继续fetch下一条记录?
请大家指教,谢谢!

回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
EXCEPTION
回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
begin
selectt.priceinto v_price ------ (v_price外部已经定义好的变量)

from cp_tab twhere t.column1 = v_bl1and t.column2=v_bl2 ;---------根据fetch的记录,查找 price
exception
when others then
v_price:=0;
end;
-------------
这样改试试
回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
加上之后
exception
when others then
v_price:=0;
end;
能fetch到下一条记录
但是没有执行selectprice的那条查询语句,后面fetch出来的,price都是0了
回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
不要用WHEN OTHERS, 用WHEN NO_DATA_FOUND THEN
回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
用WHEN NO_DATA_FOUND THEN,这样就可以继续了吗?我去试一下。。


回复

使用道具 举报

千问 | 2016-1-6 14:01:09 | 显示全部楼层
由于对应的数据记录没有的异常导致跳出循环 所以的单独写个块异常处理
selectt.priceinto v_price ------ (v_price外部已经定义好的变量)

from cp_tab twhere t.column1 = v_bl1and t.column2=v_bl2 ;---------根据fetch的记录,查找 price

改成
begin

selectt.priceinto v_price ------ (v_price外部已经定义好的变量)

from cp_tab twhere t.column1 = v_bl1and t.column2=v_bl2 ;---------根据fetch的记录,查找 price

exception

when no_data_found then

v_price:=0;

end;
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行