Oracle PL/SQL 引用游标如何

发布于 2025-01-06 05:20:53 字数 2018 浏览 1 评论 0原文

我正在尝试创建一个使用引用的 pl/sql 块。基本上,这就是我到目前为止所得到的。它们都作为单独的光标工作。我发现参考文献有点困难。我需要的是列出部门名称,然后进入emp_cursor并列出department_id =外游标部门_id中的所有员工。

set serveroutput on 
declare
  cursor dept_cursor is 
    select department_id, department_name
    from departments 
    where department_id < 100;
    dep_id departments.department_id%type;
    dep_name departments.department_name%type;

    cursor emp_cursor is 
      select last_name, job_id, hire_date, salary
      from employees 
      where employee_id < 120 and department_id =/*Need reference from deptartmens.departments_id*/department_id;
      emp_ln employees.last_name%type;
      emp_jid employees.job_id%type;
      emp_hd employees.hire_date%type;
      emp_sal employees.salary%type;

begin
    for rec_dept in dept_cursor loop
      dbms_output.put_line('Department Number: '||rec_dept.department_id|| '   Department Name: ' || rec_dept.department_name);
      dbms_output.put_line('---------------------------------------------');
      for rec_emp in emp_cursor loop
        dbms_output.put_line(rec_emp.last_name||'   ' || rec_emp.job_id ||'   ' || rec_emp.hire_date ||'   '|| rec_emp.salary);
      end loop;
      dbms_output.put_line('---------------------------------------------');
    end loop;

end ;

例如,某些输出看起来像这样。

Department Number : 10  Department Name : Administration
----------------------------------------------------------------------------------------
Department Number : 20  Department Name : Marketing
----------------------------------------------------------------------------------------
Department Number : 30  Department Name : Purchasing
Raphaely    PU_MAN   07-DEC-94   11000
Khoo    PU_CLERK   18-MAY-95   3100
Baida    PU_CLERK   24-DEC-97   2900
Tobias    PU_CLERK   24-JUL-97   2800
Himuro    PU_CLERK   15-NOV-98   2600
Colmenares    PU_CLERK   10-AUG-99   2500
----------------------------------------------------------------------------------------

I'm trying to create a pl/sql block that uses references. Basicly, this is what I've got so far. They both work as individual cursors. I'm finding it a little difficult with the reference. What I need is to list out department name, then go through to the emp_cursor and list all of the employees that are in that are in department_id = outer cursor department _id.

set serveroutput on 
declare
  cursor dept_cursor is 
    select department_id, department_name
    from departments 
    where department_id < 100;
    dep_id departments.department_id%type;
    dep_name departments.department_name%type;

    cursor emp_cursor is 
      select last_name, job_id, hire_date, salary
      from employees 
      where employee_id < 120 and department_id =/*Need reference from deptartmens.departments_id*/department_id;
      emp_ln employees.last_name%type;
      emp_jid employees.job_id%type;
      emp_hd employees.hire_date%type;
      emp_sal employees.salary%type;

begin
    for rec_dept in dept_cursor loop
      dbms_output.put_line('Department Number: '||rec_dept.department_id|| '   Department Name: ' || rec_dept.department_name);
      dbms_output.put_line('---------------------------------------------');
      for rec_emp in emp_cursor loop
        dbms_output.put_line(rec_emp.last_name||'   ' || rec_emp.job_id ||'   ' || rec_emp.hire_date ||'   '|| rec_emp.salary);
      end loop;
      dbms_output.put_line('---------------------------------------------');
    end loop;

end ;

For example some output would look like this.

Department Number : 10  Department Name : Administration
----------------------------------------------------------------------------------------
Department Number : 20  Department Name : Marketing
----------------------------------------------------------------------------------------
Department Number : 30  Department Name : Purchasing
Raphaely    PU_MAN   07-DEC-94   11000
Khoo    PU_CLERK   18-MAY-95   3100
Baida    PU_CLERK   24-DEC-97   2900
Tobias    PU_CLERK   24-JUL-97   2800
Himuro    PU_CLERK   15-NOV-98   2600
Colmenares    PU_CLERK   10-AUG-99   2500
----------------------------------------------------------------------------------------

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最单纯的乌龟 2025-01-13 05:20:53

你需要的是一个游标变量:

cursor emp_cursor ( v_dept_id number ) is 
  select last_name, job_id, hire_date, salary
  from employees 
  where employee_id < 120 and department_id = v_dept_id;

然后:

for rec_emp in emp_cursor loop

变成:

for rec_emp in emp_cursor(rec_dept.department_id) loop

What you need is a cursor variable:

cursor emp_cursor ( v_dept_id number ) is 
  select last_name, job_id, hire_date, salary
  from employees 
  where employee_id < 120 and department_id = v_dept_id;

Then:

for rec_emp in emp_cursor loop

Becomes:

for rec_emp in emp_cursor(rec_dept.department_id) loop
救星 2025-01-13 05:20:53

您可以将 EMP_CURSOR 声明为参数化游标。类似于

declare
  cursor dept_cursor is
    select department_id, department_name
    from departments
    where department_id < 100;
    dep_id departments.department_id%type;
    dep_name departments.department_name%type;
  cursor emp_cursor( p_department_id IN NUMBER ) is
    select last_name, job_id, hire_date, salary
    from employees
    where employee_id < 120
      and department_id = p_department_id;
  emp_ln employees.last_name%type;
  emp_jid employees.job_id%type;
  emp_hd employees.hire_date%type;
  emp_sal employees.salary%type;
begin
  for rec_dept in dept_cursor loop
    dbms_output.put_line('Department Number: '||rec_dept.department_id|| '   Department Name: ' || rec_dept.department_name);
    dbms_output.put_line('---------------------------------------------');
    for rec_emp in emp_cursor( rec_dept.department_id ) loop
      dbms_output.put_line(rec_emp.last_name||'   ' || rec_emp.job_id ||'   ' || rec_emp.hire_date ||'   '|| rec_emp.salary);
    end loop;
    dbms_output.put_line('---------------------------------------------');
  end loop;
end ;

HR 模式中生成以下输出的内容

Department Number: 10   Department Name: Administration
---------------------------------------------
---------------------------------------------
Department Number: 20   Department Name: Marketing
---------------------------------------------
---------------------------------------------
Department Number: 30   Department Name: Purchasing
---------------------------------------------
Raphaely   PU_MAN   07-DEC-02   11000
Khoo   PU_CLERK   18-MAY-03   3100
Baida   PU_CLERK   24-DEC-05   2900
Tobias   PU_CLERK   24-JUL-05   2800
Himuro   PU_CLERK   15-NOV-06   2600
Colmenares   PU_CLERK   10-AUG-07   2500
---------------------------------------------
Department Number: 40   Department Name: Human Resources
---------------------------------------------
---------------------------------------------
Department Number: 50   Department Name: Shipping
---------------------------------------------
---------------------------------------------
Department Number: 60   Department Name: IT
---------------------------------------------
Hunold   IT_PROG   03-JAN-06   9000
Ernst   IT_PROG   21-MAY-07   6000
Austin   IT_PROG   25-JUN-05   4800
Pataballa   IT_PROG   05-FEB-06   4800
Lorentz   IT_PROG   07-FEB-07   4200
---------------------------------------------
Department Number: 70   Department Name: Public Relations
---------------------------------------------
---------------------------------------------
Department Number: 80   Department Name: Sales
---------------------------------------------
---------------------------------------------
Department Number: 90   Department Name: Executive
---------------------------------------------
King   AD_PRES   17-JUN-03   24000
Kochhar   AD_VP   21-SEP-05   17000
De Haan   AD_VP   13-JAN-01   17000
---------------------------------------------

现在,从性能的角度来看,您最好连接两个表,而不是在 PL/SQL 中编写自己的嵌套循环。

You can declare EMP_CURSOR as a parameterized cursor. Something like

declare
  cursor dept_cursor is
    select department_id, department_name
    from departments
    where department_id < 100;
    dep_id departments.department_id%type;
    dep_name departments.department_name%type;
  cursor emp_cursor( p_department_id IN NUMBER ) is
    select last_name, job_id, hire_date, salary
    from employees
    where employee_id < 120
      and department_id = p_department_id;
  emp_ln employees.last_name%type;
  emp_jid employees.job_id%type;
  emp_hd employees.hire_date%type;
  emp_sal employees.salary%type;
begin
  for rec_dept in dept_cursor loop
    dbms_output.put_line('Department Number: '||rec_dept.department_id|| '   Department Name: ' || rec_dept.department_name);
    dbms_output.put_line('---------------------------------------------');
    for rec_emp in emp_cursor( rec_dept.department_id ) loop
      dbms_output.put_line(rec_emp.last_name||'   ' || rec_emp.job_id ||'   ' || rec_emp.hire_date ||'   '|| rec_emp.salary);
    end loop;
    dbms_output.put_line('---------------------------------------------');
  end loop;
end ;

which generates the following output in the HR schema

Department Number: 10   Department Name: Administration
---------------------------------------------
---------------------------------------------
Department Number: 20   Department Name: Marketing
---------------------------------------------
---------------------------------------------
Department Number: 30   Department Name: Purchasing
---------------------------------------------
Raphaely   PU_MAN   07-DEC-02   11000
Khoo   PU_CLERK   18-MAY-03   3100
Baida   PU_CLERK   24-DEC-05   2900
Tobias   PU_CLERK   24-JUL-05   2800
Himuro   PU_CLERK   15-NOV-06   2600
Colmenares   PU_CLERK   10-AUG-07   2500
---------------------------------------------
Department Number: 40   Department Name: Human Resources
---------------------------------------------
---------------------------------------------
Department Number: 50   Department Name: Shipping
---------------------------------------------
---------------------------------------------
Department Number: 60   Department Name: IT
---------------------------------------------
Hunold   IT_PROG   03-JAN-06   9000
Ernst   IT_PROG   21-MAY-07   6000
Austin   IT_PROG   25-JUN-05   4800
Pataballa   IT_PROG   05-FEB-06   4800
Lorentz   IT_PROG   07-FEB-07   4200
---------------------------------------------
Department Number: 70   Department Name: Public Relations
---------------------------------------------
---------------------------------------------
Department Number: 80   Department Name: Sales
---------------------------------------------
---------------------------------------------
Department Number: 90   Department Name: Executive
---------------------------------------------
King   AD_PRES   17-JUN-03   24000
Kochhar   AD_VP   21-SEP-05   17000
De Haan   AD_VP   13-JAN-01   17000
---------------------------------------------

Now, from a performance standpoint, you would be much better served to join the two tables rather than writing your own nested loop in PL/SQL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文