如果找不到列,请获取光标的下一个值?

时间:2014-02-11 09:59:51

标签: oracle plsql

我正在使用记录循环游标,但是当此循环无法找到值时,
它停止执行脚本。那么如果记录为空,还是让循环继续吗?

declare 
cust_id info.CUSTOMER_ID%type; 
SPNum info.spnumber%type; 
EnterTicketDate EnteryDate%type;

cursor mycursor is 
select S_P_NUM ,_START_TIME
from info
where UPPER (STATUS)='YES';

begin 

for myRecord in mycursor
loop
dbms_output.put_line(SPNum);
select PID into cust_id from info where dn_num=SPNum;
dbms_output.put_line('ID is '|| cust_id);
end loop;
end;

我正在考虑通过处理data_not_found来避免这种情况,并移动到循环的下一个值,但是如何可能?

我可以使用nextval关键字吗?

declare 
cust_id info.CUSTOMER_ID%type; 
SPNum info.spnumber%type; 
EnterTicketDate EnteryDate%type;

cursor mycursor is 
select S_P_NUM ,_START_TIME
from info
where UPPER (STATUS)='YES';

begin 

for myRecord in mycursor
loop
dbms_output.put_line(SPNum);
select PID into cust_id from info where dn_num=SPNum;
dbms_output.put_line('ID is '|| cust_id);
exit when myRecord%notfound;
end loop;

exception 
when data_not_found
-- here is a recall for the next value of my record 
end;

1 个答案:

答案 0 :(得分:2)

CURSOR返回NO_DATA_FOUND时,意味着无法再从中读取数据。

for myRecord in mycursor
loop
-- exit when myRecord%notfound;
/* NOT NEEDED as FOR LOOP itself will terminate on NO DATA in mycursor */
dbms_output.put_line(SPNum);
select PID into cust_id from info where dn_num=SPNum;
dbms_output.put_line('ID is '|| cust_id);
end loop;

希望你需要这个。

declare 
cust_id info.CUSTOMER_ID%type; 
SPNum info.spnumber%type; 
EnterTicketDate EnteryDate%type;

cursor mycursor is 
select S_P_NUM ,_START_TIME
from info
where UPPER (STATUS)='YES';

begin 
for myRecord in mycursor
loop
  IF(myRecord.S_P_NUM IS NOT NULL)
  THEN
     BEGIN
       dbms_output.put_line(myRecord.S_P_NUM);
       select PID into cust_id from info where dn_num=myRecord.S_P_NUM;
       dbms_output.put_line('ID is '|| cust_id);
     EXCEPTION WHEN NO_DATA_FOUND THEN
       dbms_output.put_line('NOT FOUND FOR SPNum '|| myRecord.S_P_NUM);
       CONTINUE; -- Process Next S_P_NUM from cursor
     END;
  END IF;
end loop;
END;