【发布时间】: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