Oracle PL/SQL 引用游标如何
我正在尝试创建一个使用引用的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你需要的是一个游标变量:
然后:
变成:
What you need is a cursor variable:
Then:
Becomes:
您可以将
EMP_CURSOR
声明为参数化游标。类似于在
HR
模式中生成以下输出的内容现在,从性能的角度来看,您最好连接两个表,而不是在 PL/SQL 中编写自己的嵌套循环。
You can declare
EMP_CURSOR
as a parameterized cursor. Something likewhich generates the following output in the
HR
schemaNow, 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.