2017-08-08 16 views
0

爲什麼我得到下面的錯誤,請告訴我代碼中出現了什麼問題。 ORA-06511:PL/SQL:遊標已經打開在程序中使用光標

代碼:

create or replace procedure student_info IS 
c_id student_details.student_id%type ;  
c_name student_details.student_name%type; 
c_status student_details.student_status%type; 

cursor stu_c1 IS 
select * from student_details 
where student_status='Absent'; 
begin 
open stu_c1; 
Fetch stu_c1 into c_id,c_name,c_status ; 
for rec in stu_c1 loop 
insert into student_data values (c_status,c_id,c_name); 
commit; 
end loop; 
CLOSE stu_c1;  
end; 

回答

1

如果使用FOR循環與顯式遊標,你不需要open/fetch/close/handle NOTFOUND

... 
begin 
--open stu_c1; -- extra, not needed 
--Fetch stu_c1 into c_id,c_name,c_status ; -- extra, not needed 
for rec in stu_c1 loop -- that's enough 
insert into student_data values (c_status,c_id,c_name); 
.... 
end loop; 
--CLOSE stu_c1; -- not needed 

或者,你就打開,提取,關閉,處理NOTFOUND,但不能使用FOR ... IN

+0

謝謝,但數據沒有被插入到「student_data」表中,因爲我們沒有給c_status分配任何值,c- – Ali

+0

您是否忘記了提交?另外,檢查語法。使用'FOR'循環,您需要像insert一樣插入student_data值(rec.student_status,rec.id,rec.name); '假設student_status,id和name是'student_details'表中的字段 – a1ex07