首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >游标plsql使用select

问游标plsql使用select
EN

Stack Overflow用户
提问于 2020-02-01 14:01:49
回答 1查看 58关注 0票数 0

我想从table1(etudiant)中得到一个值,以便在table2(employee)中使用slect,我使用游标,但是我没有得到结果;您能帮我谢谢吗?

代码语言:javascript
复制
    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;
/
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-01 16:05:23

看来你太复杂了。为什么不直接加入etudiant和employee表?就像这样:

代码语言:javascript
复制
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一起使用。
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60017581

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档