gpt4 book ai didi

sql - Oracle PL/SQL 引用游标如何

转载 作者:行者123 更新时间:2023-12-04 05:57:50 25 4
gpt4 key购买 nike

我正在尝试创建一个使用引用的 pl/sql 块。基本上,这就是我到目前为止所得到的。它们都作为单独的游标工作。我发现引用文献有点困难。我需要的是列出部门名称,然后转到emp_cursor并列出所有在部门 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
----------------------------------------------------------------------------------------

最佳答案

您可以申报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 中编写自己的嵌套循环更好。

关于sql - Oracle PL/SQL 引用游标如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9304644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com