【问题标题】:How to pass parameters to the cursor and fetch the records from the two tables?如何将参数传递给游标并从两个表中获取记录?
【发布时间】:2026-01-08 08:55:01
【问题描述】:

在循环中,使用游标检索 deptNumber 和 deptName 从“部门”表中,将 deptNumber 传递给引用游标以检索 从名为的表中属于该 deptNumber 的员工 “员工”。如何将参数传递给游标并从Employee表中获取记录?

    set serveroutput on;
declare
cursor dept_cursor(deptid hr.departments.department_id%type) is 
select department_id, department_name from hr.departments where department_id = deptid;
deptno hr.departments.department_id%type;
deptname hr.departments.department_name%type;

cursor emp_cursor(deptid hr.departments.department_id%type) is
select employee_id, first_name, salary, job_id from hr.employees where department_id = deptid;
empno hr.employees.employee_id%type;
empname hr.employees.first_name%type;
sal hr.employees.salary%type;
jobid hr.employees.job_id%type;

begin
open dept_cursor(10);
loop
fetch dept_cursor into deptno, deptname;
exit when dept_cursor%notfound;
dbms_output.put_line('Department no : '|| deptno||' Department name : '||deptname);
end loop;
close dept_cursor;
open emp_cursor(10);
loop
fetch emp_cursor into empno, empname, sal, jobid;
exit when emp_cursor%notfound;
dbms_output.put_line('Employee no : '|| empno||' Employee name : '||empname ||'Employee Salary :' ||sal||' Job :'||jobid);
end loop;
close emp_cursor;

end;

【问题讨论】:

  • 在表中完成修改以从一个表中获取结果。虽然要从另一个表中获取的记录是未决的。
  • 设置服务器输出;声明游标 dept_cursor(deptid hr.departments.department_id%type) 是 select department_id, department_name from hr.departments where department_id = deptid; dept_rec hr.departments%rowtype;开始打开 ​​dept_cursor(10);循环获取 dept_cursor 到 dept_rec;当 dept_cursor%notfound 时退出; dbms_output.put_line('部门编号:'||dept_rec.department_id||'部门名称:'||dept_rec.department_name);结束循环;关闭 dept_cursor;结束;
  • 您使用的 SQL 客户端(Oracle SQL Developer 或 PL/SQL Developer)与问题完全无关。那么为什么要添加这些标签呢?

标签: oracle plsql oracle-sqldeveloper plsqldeveloper


【解决方案1】:

我没有你的表,所以我使用了 Scott 的示例模式。阅读代码中的 cmets(并且不要将您自己的代码作为评论发布;我猜您已经看到它不可读)。

SQL> set serveroutput on
SQL> declare
  2    l_rc      sys_refcursor;
  3    -- variables which will be used to display ref cursor's values
  4    l_empno   emp.empno%type;
  5    l_ename   emp.ename%type;
  6    l_sal     emp.sal%type;
  7  begin
  8    -- loop through all departments, using cursor FOR loop
  9    for cur_r in (select deptno from dept) loop
 10
 11      -- fetch employees (for that department) into a ref cursor
 12      open l_rc for select empno, ename, sal
 13                    from emp
 14                    where deptno = cur_r.deptno;
 15
 16      -- display what's being fetched
 17      dbms_output.put_line('Department ' || cur_r.deptno);
 18      loop
 19        fetch l_rc into l_empno, l_ename, l_sal;
 20        exit when l_rc%notfound;
 21        dbms_output.put_line('  ' || l_empno ||': '|| rpad(l_ename, 10, ' ') || l_sal);
 22      end loop;
 23    end loop;
 24  end;
 25  /

导致

Department 10
- 7782: CLARK     2450
- 7839: KING      5000
- 7934: MILLER    1300
Department 20
- 7369: SMITH     800
- 7566: JONES     2975
- 7788: SCOTT     3000
- 7876: ADAMS     1100
- 7902: FORD      3000
Department 30
- 7499: ALLEN     1600
- 7521: WARD      1250
- 7654: MARTIN    1250
- 7698: BLAKE     2850
- 7844: TURNER    1500
- 7900: JAMES     950
Department 40

PL/SQL procedure successfully completed.

SQL>

【讨论】:

  • 谢谢,但看起来很复杂。有没有其他方法可以做到这一点?
  • 复杂?它有什么复杂的?它可以满足您的要求。使用游标检索部门并将其(一个部门)传递给返回所需结果的引用游标。