--6.(选做)使用LOOP循环求1-100之间的素数
--7.打印99乘法表 declare
-- Local variables here i integer; j integer; begin
for i in 1..9 loop for j in 1..i loop
dbms_output.put(i||'*'||j||'='||(i*j)||' '); end loop;
dbms_output.put_line(''); end loop; end;
--8.根据工资查询员工姓名。如果此员工不存在(发出NO_DATA_FOUND异常),打印相应的提示信息。 declare
cursor c_name(p_salary employees.salary%type) is
select last_name from employees t where t.salary=p_salary; begin
for names in c_name('&v_salary') loop
dbms_output.put_line(names.last_name); end loop; exception
when no_data_found then dbms_output.put_line('没有改工资对应的员工'); end;
--9.显示EMP中的第四条记录。游标%rowcount=4 declare
cursor c_emp is
select * from employees t ; begin
for rec in c_emp loop
if(c_emp%rowcount=4) then
dbms_output.put_line(rec.last_name); end if; end loop; exception
when no_data_found then dbms_output.put_line('没有改工资对应的员工'); end; /*
10.根据部门名称(由用户输入),按以下格式打印各部门人员姓名: 部门名称:RESEARCH
部门人员:SMITH,JONES,FORD */
declare
v_ename employees.last_name%type; v_ename_str varchar2(1000);
v_dname departments.department_name%type; --部门名称 cursor c_emp(dname varchar2) is SELECT e.last_name FROM employees e,departments e.department_id=d.department_id and department_name=dname; begin
v_dname := '&请输入部门名称:'; --v_dname := 'Marketing';
dbms_output.put_line('部门名称:'||v_dname); open c_emp(v_dname); loop
fetch c_emp into v_ename; exit when c_emp%notfound;
--dbms_output.put(v_ename||',');--put会放缓冲区 v_ename_str := v_ename_str||v_ename||','; end loop;
v_ename_str := substr(v_ename_str,1,length(v_ename_str)-1); dbms_output.put_line('部门人员:'||v_ename_str); --dbms_output.new_line;--刷新缓冲区 end; /*
11.针对所有部门,按以下格式打印各部门人员姓名: 部门名称:RESEARCH 部门人员:SMITH,JONES,FORD
部门名称:ACCOUNTING 部门人员:CLARK,KING,MILLER,
如果该部门没有人员,则输出: 部门名称:Treasury
部门人员:该部门没有员工 实现提示: 1)循环每个部门,用其部门号作条件去查员工表 2)用显示cursor完成 3)要求用FOR,会用到嵌套循环。 */
DECLARE
cursor c_department
d where
is select * from departments;
cursor c_emp(p_deptno employees.department_id%type)
is select * from employees where department_id=p_deptno; v_empname varchar2(1000); i integer:=0; begin
for dept in c_department loop
dbms_output.put_line('部门名称:'||dept.department_name); dbms_output.put('部门人员:'); v_empname:=''; i:=0;
for emp in c_emp(dept.department_id) loop
v_empname:=v_empname||emp.last_name||','; i:=i+1; end loop;
if(i=0) then
dbms_output.put_line('该部门没有员工'); else
dbms_output.put_line(substr(v_empname,1,length(v_empname)-1)); end if; end loop; end; /*
12.对所有员工,如果该员工职位是MANAGER,并且在DALLAS工作那么就给他薪金加15%;如果该员工职位是CLERK,并且在NEW
YORK工作那么就给他薪金扣除5%;其他情况不作处理*/ declare
cursor c_emp is
select t.employee_id,t.job_id,l.city
from employees t ,departments d ,locations l
where t.department_id=d.department_id and d.location_id=l.location_id; begin
for rec in c_emp loop
if(REGEXP_LIKE (rec.job_id, '.*_MGR') and rec.city='DALLAS') then update employees t set t.salary=t.salary*(1.15) where t.employee_id=rec.employee_id; end if;
if (REGEXP_LIKE (rec.job_id, 'S[HT]_CLERK') and rec.city='NEW YORK') then update employees t set t.salary=t.salary*0.95 where t.employee_id=rec.employee_id; end if; end loop;
end; /*
13.对直接上级是'BLAKE'的所有员工,按照参加工作的时间加薪: 81年6月以前的加薪10% 81年6月以后的加薪5% */ declare
cursor c_emp is select t.*
from employees t
where t.manager_id in (select employee_id from employees where last_name = 'BLAKE'); begin
for rec in c_emp loop
if rec.hire_date
update employees t set t.salary=t.salary*1.1 where t.employee_id=rec.employee_id; end if;
if rec.hire_date>=to_date('19810601','yyyymmdd') then update employees t set t.salary=t.salary*1.05 where t.employee_id=rec.employee_id; end if; end loop; end;