我想从table1(etudiant)中得到一个值,以便在table2(employee)中使用slect,我使用游标,但是我没有得到结果;您能帮我谢谢吗?
declare
cle employee.id%type;
cursor cur_test is select id from etudiant where name ='Hichem';
no_data_found exception;
begin
open cur_test;
loop
fetch cur_test into cle;
exit when cur_test%notfound;
end loop;
--dbms_output.put_line('ID IS ' ||cle);
select * from employee where id=cle;
if cur_test%rowcount=0 then
close cur_test;
raise no_data_found;
end if;
close cur_test;
exception
when no_data_found then
dbms_output.put_line('no data found');
end;
/发布于 2020-02-01 16:05:23
看来你太复杂了。为什么不直接加入etudiant和employee表?就像这样:
begin
for cur_r in (select e.name
from employee e join etudiant t on t.id = e.id
where t.name = 'Hichem'
)
loop
dbms_output.put_line(cur_r.name);
end loop;
end;
/也许您甚至不需要一个循环;或者,如果有多个具有您要查找的名称的人(本例中为Hichem),则需要循环。
根据您的代码:
一般来说,使用游标FOR循环比手动执行所有操作(声明游标变量、打开游标、获取、退出循环、关闭游标)要简单得多--如果您使用游标for循环,则exception
select * from employees为您做了所有这些操作,而不需要显式声明no_data_found;这是一个预定义的错误,因为在PL/SQL中,您必须选择结果INTO。如果期望的行超过一行,请考虑将集合与bulk collect一起使用。https://stackoverflow.com/questions/60017581
复制相似问题