当前位置:Gxlcms > 数据库问题 > PL/SQL 游标 (实验七)

PL/SQL 游标 (实验七)

时间:2021-07-01 10:21:17 帮助过:10人阅读

PL/SQL 游标

emp、dept 目标表结构及数据

技术分享图片

要求

  • 基于部门表建立游标dept_cursor1,使用记录变量接收游标数据,输出部门表信息;
    显示格式:
    部 门 号: XXX
    部门名称: XXX
    所在位置: XXX

  • 基于部门表建立游标dept_cursor2,使用标量变量接收游标数据,输出部门表信息;
    显示格式:部门号:XXX 部门名称:XXX 所在位置:XXX

  • 基于雇员表建立游标emp_cursor,根据用户输入的部门号,输出该部门薪水在5000元上的雇员姓名、薪水.
    显示格式:雇员姓名:XXX 薪水:XXX

1.基于部门表建立游标dept_cursor1,使用记录变量接收游标数据,输出部门表信息;
显示格式:
部 门 号: XXX
部门名称: XXX
所在位置: XXX

declare
  cursor dept_cursor1 is
  select dno,dname,loc from dept;
  
  dept_record dept%rowtype;

begin
  open dept_cursor1;
  loop
       fetch dept_cursor1 into dept_record;
       
       exit when dept_cursor1%notfound or dept_cursor1%rowcount > 3;
       dbms_output.put_line('部门号:'||dept_record.dno);
       dbms_output.put_line('部门名称:'||dept_record.dname);
       dbms_output.put_line('所在位置:'||dept_record.loc);
       
  end loop;
  close dept_cursor1;
end;
       

2.基于部门表建立游标dept_cursor2,使用标量变量接收游标数据,输出部门表信息;
显示格式:部门号:XXX 部门名称:XXX 所在位置:XXX

declare
  cursor dept_cursor2 is
  select dno,dname,loc from dept;
  v_deptno dept.dno%type;
  v_deptname dept.dname%type;
  v_deptloc dept.loc%type;

begin
  open dept_cursor2;
  loop
       fetch dept_cursor2 into v_deptno,v_deptname,v_deptloc;
       
       exit when dept_cursor2%notfound or dept_cursor2%rowcount > 3;
       dbms_output.put_line('部门号:'||v_deptno||',部门名称:'||v_deptname||',所在地:'||v_deptloc);
       
       
  end loop;
  close dept_cursor2;
end;
       

3.基于雇员表建立游标emp_cursor,根据用户输入的部门号,输出该部门薪水在5000元上的雇员姓名、薪水.
显示格式:雇员姓名:XXX 薪水:XXX

declare
  cursor emp_cursor is
  select ename,sal from  emp where dno = &dno;
  v_ename emp.ename%type;
  v_sal emp.sal%type;
  
begin  
  
 open emp_cursor;
    loop
       fetch emp_cursor into v_ename,v_sal;
       exit when emp_cursor%notfound;
       if v_sal > 5000 
       then 
         dbms_output.put_line('雇员姓名:'||v_ename||',薪水:'||v_sal);  
       end if;
   end loop;
  close emp_cursor;

end;
  

PL/SQL 游标 (实验七)

标签:用户   sel   sql   sql 游标   declare   记录   格式   etc   out   

人气教程排行