【发布时间】:2017-03-03 11:39:22
【问题描述】:
在HR Schema 中,我正在编写一个PL/SQL 块来获取并显示所有employee_id 从150 到200 的员工。
declare
v_c number(3) := 150 ;
v_fn varchar2(150);
begin
for i in v_c .. v_c + 50
loop
select first_name into v_fn from employees where employee_id = i;
dbms_output.put_line(i || ' ' || v_fn);
end loop;
end;
但是,如果有从 150 到 200 的所有 employee_id 的数据,它就可以正常工作。
假设我缺少employee_id = 160 的数据,那么这就是输出。
Error report -
ORA-01403: no data found
ORA-06512: at line 7
01403. 00000 - "no data found"
*Cause: No data was found from the objects.
*Action: There was no data from the objects which may be due to end of fetch.
150 Peter
151 David
152 Peter
153 Christopher
154 Nanette
155 Oliver
156 Janette
157 Patrick
158 Allan
159 Lindsey
如何跳过 160 的错误并显示其他员工直到 employee_id = 200?
When the select query fails, The execution of the loop must continue.
注意:我无可救药地尝试在 EXCEPTION 中使用 GOTO。
【问题讨论】:
-
@VáclavKužel 感谢您的宝贵意见。我使用了游标并避免了性能问题。顺便说一句,e.id 不存在。 e.employee_id 存在
标签: oracle for-loop select plsql