--小测:输出各部门员工的部门名称和工号、姓名,工资。
<wbr>要求:<br><wbr>各部门工资最高的放在前面<br><wbr>在“DALLAS”地方的部门名称后加上“*”。<br> ·三种实现方式<br> 1 通过显式<span style="color:black">游标</span><br> 2 通过<span style="color:black">FOR循环</span><br> 3 通过动态SQL方式</wbr></wbr></wbr>
<wbr></wbr>
<wbr>----------------------------------------显示<span style="color:black">游标</span></wbr>
declare
type emp_dept is record(
name dept.dname%type,
loc dept.loc%type,
empno emp.empno%type,
ename emp.ename%type,
sal emp.sal%type);
emp_table emp_dept;
cursor cur_emp is
select d.dname, d.loc, e.empno, e.ename, e.sal
from emp e, dept d
where d.deptno = e.deptno
order by e.deptno, e.sal desc;
begin
open cur_emp;
fetch cur_emp
into emp_table;
loop
if emp_table.loc = 'DALLAS' THEN
dbms_output.put_line(emp_table.loc || '*' || '-->' || emp_table.name ||
'-->' || emp_table.empno || '-->' ||
emp_table.sal);
else
dbms_output.put_line(emp_table.loc || '-->' || emp_table.name ||
'-->' || emp_table.empno || '-->' ||
emp_table.sal);
end if;
fetch cur_emp
into emp_table;
exit when cur_emp%notfound;
end loop;
close cur_emp;
end;
----不用if else
begin
for x in (select(case when d.loc='DALLAS'
then d.loc||'*' else d.loc
end),d.dname,e.empno,e.ename,e.sal from emp e,dept d where d.deptno=e.deptno order by e.deptno,e.sal desc)
loop
dbms_output.put_line(x.loc||'-->'||x.dname||'-->'||x.empno||'-->'||x.sal);
end loop;
END;
------------------------------- for循环
begin
for x in (select d.dname,d.loc,e.empno,e.ename,e.sal from emp e,dept d where d.deptno=e.deptno order by e.deptno,e.sal desc)
loop
if x.loc='DALLAS' then
x.loc:=x.loc||'*';
end if;
dbms_output.put_line(x.loc||'-->'||x.dname||'-->'||x.empno||'-->'||x.sal);
end loop;
END;
<wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">-------------------------------动态sql</span><br><p style="margin-top:0px; margin-bottom:5px; padding-top:0px; padding-bottom:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px; color:rgb(70,70,70); font-family:simsun; font-size:14px; text-align:left"> <span style="color:black"><span style="font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span></span></span></p> <pre name="code" class="sql"><span style="background-color: rgb(255, 255, 255);">declare cursor cur_emp is select d.dname, d.loc, e.empno, e.ename, e.sal from emp e, dept d where d.deptno = e.deptno order by e.deptno, e.sal desc; type emp_table_type is table of cur_emp%rowtype index by binary_integer; emp_table emp_table_type; str_sql varchar2(1000); begin str_sql := 'select d.dname, d.loc, e.empno, e.ename, e.sal from emp e, dept d where d.deptno = e.deptno order by e.deptno, e.sal desc'; execute immediate str_sql bulk collect into emp_table; for i in 1 .. emp_table.count loop if emp_table(i).loc = 'DALLAS' THEN dbms_output.put_line(emp_table(i).loc || '*' || '-->' || emp_table(i).dname || '-->' || emp_table(i).empno || '-->' || emp_table(i).sal); else dbms_output.put_line(emp_table(i).loc || '-->' || emp_table(i).dname || '-->' || emp_table(i).empno || '-->' || emp_table(i).sal); end if; end loop; end;</span></pre> <span style="background-color:rgb(255,255,255)"><br><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">----还可以用记录类型来做 如上</span></wbr></span><br></wbr>