gpt4 book ai didi

oracle - 具有最终记录副本的 Oracle 游标

转载 作者:行者123 更新时间:2023-12-02 21:40:08 28 4
gpt4 key购买 nike

Why am getting duplicate records ? pls correct me.Thanks in Advance.

declare
clazzes_rec clazzes%rowtype;
cursor clazzes_cur is select * from clazzes;
begin
if clazzes_cur%isopen then
dbms_output.put_line('Cursor is not open,trying to Open.... ... .. .');
end if;

open clazzes_cur;

dbms_output.put_line('Cursor opened :');

loop
fetch clazzes_cur into clazzes_rec;
dbms_output.put_line('id:'||clazzes_rec.id||':name:'||clazzes_rec.name);
exit when clazzes_cur%notfound;
end loop;

close clazzes_cur;

end;

输出:

Cursor opened :
id:1:name:leo1
id:2:name:leo2
id:3:name:leo3
id:4:name:leo4
id:4:name:leo4

PL/SQL procedure successfully completed

最佳答案

只需交换行:

loop
fetch clazzes_cur into clazzes_rec;
exit when clazzes_cur%notfound;
dbms_output.put_line('id:'||clazzes_rec.id||':name:'||clazzes_rec.name);
end loop;

当您获取最后一条记录并尝试获取下一条 clazzes_cur%notfound 时,它会变为 true,但在它有机会退出循环之前,您将再次输出最后一条记录。

关于oracle - 具有最终记录副本的 Oracle 游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20586681/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com