编写一个PL/SQL块,修改员工号为201的员工工资为8000元,保证修改后的工资在职位允许的范围内,否则不予操作,并说明原因
设计思路
1.定义最高工资和最低工资字段变量
2.修改员工号为201号员工的工资。
3.将员工号是201号员工所对应的职位号的最高工资和最低工资into到相应变量
4.若8000元不在范围之内则不予操作并提示。
set serveroutput on
declare
sal_min employees.salary%type;
sal_max employees.salary%type;
begin
select jobs.max_salary,jobs.min_salary into sal_max,sal_min
from jobs,employees
where jobs.job_id=employees.job_id and employees.employee_id=201;
if 8000 between sal_max and sal_min
then
update employees set salary=8000 where employee_id=201;
else dbms_output.put_line('不在允许的范围之内');
end if;
end;
编写一个PL/SQL块,修改员工号为201的员工工资为8000元,保证修改后的工资在职位允许的范围内,否则在修改后取消操作,并说明原因
set serveroutput on;
declare
sal_max employees.salary%type;
sal_min employees.salary%type;
sal employees.salary%type;
e exception;
begin
select jobs.max_salary,jobs.min_salary into sal_max,sal_min
from jobs,employees
where jobs.job_id=employees.job_id and employees.employee_id=201;
update employees set salary=8000 where employee_id=201
returning salary into sal;
if sal not between sal_max and sal_min then
raise e;
end if;
exception
when e then
dbms_output.put_line('工资不在最低及最高范围内,请重新操作!');
rollback;
end;
编写一个PL/SQL块。对所有员工按他们的工资的10%判断。如果大于300就不加薪。否则加10%。同时 给与提示
set serveroutput on;
declare
cursor e_sal is select * from employees;
sal employees.salary%type;
empid employees. employee_id %type;
begin
for employee in e_sal loop
sal :=employee.salary*0.1;
if sal>300 then
dbms_output.put_line('不可以加薪');
else
dbms_output.put_line('可以加薪');
empid :=employee.employee_id;
dbms_output.put_line(empid);
update employees set salary=salary+sal where employee_id=empid;
end if;
end loop;
end;
编写一个PL/SQL块,输出所有员工的员工姓名、员工号、工资和部门号
set serveroutput on;
begin
for v_emp in (select employee_id, last_name from employees ) loop
dbms_output.put_line(v_emp.employee_id||''||v_emp.last_name);
end loop;
end;
编写一个PL/SQL块,输出所有比本部门平均工资高的员工信息
set serveroutput on;
declare
v_avgsal employees.salary%type;
begin
for v_emp in (select * from employees) loop
select avg(salary) into v_avgsal from employees
where department_id=v_emp.department_id;
if v_emp.salary>v_avgsal then
dbms_output.put_line(v_emp.first_name||' '||v_emp.last_name||' '||
v_emp.employee_id||' '||v_emp.salary||' '||v_emp.department_id);
end if;
end loop;
end;
编写一个PL/SQL块,输出所有员工及其部门领导的姓名、员工号及其部门号
set serveroutput on;
declare
cursor c_emp is
select e.employee_id eid,e.last_name ename,e.department_id edid,
m.employee_id mid,m.last_name mname
from employees e,employees m
where e.manager_id=m.employee_id;
begin
for v_emp in c_emp loop
dbms_output.put_line(v_emp.eid||' '||v_emp.ename||' '||
v_emp.edid||' '||v_emp.mid||' '||v_emp.mname);
end loop;
end;
查询last_name 是Smith的员工工资,如果该员工不存在,则输出“There is not such an employee!”;如果存在多个同名的员工,提示该姓有多名员工,同时输出其员工号和工资
set serveroutput on;
declare
v_emp employees%rowtype;
begin
select * into v_emp from employees where last_name = 'Smith';
dbms_output.put_line(v_emp.employee_id||''||v_emp.first_name||''|| v_emp.last_name||' '||v_emp.salary);
exception
when no_data_found then
dbms_output.put_line('There is not such an employee!');
when too_many_rows then
for v_emp in (select * from employees where last_name = 'Smith') loop
dbms_output.put_line(v_emp.employee_id||' '||v_emp.first_name||' '|| v_emp.last_name||' '||v_emp.salary);
end loop;
end;
用用户定义异常方法设计修改108员工的工资,保证修改后工资不超过6000
set serveroutput on;
DECLARE
e_highlimit EXCEPTION;
v_sal employees.salary%TYPE;
BEGIN
UPDATE employees SET salary=salary+100
WHERE employee_id=108 RETURNING salary INTO v_sal;
IF v_sal>6000 THEN
RAISE e_highlimit;
END IF;
EXCEPTION
WHEN e_highlimit THEN
DBMS_OUTPUT.PUT_LINE('The salary is too large! ');
ROLLBACK;
END;
不用异常方式
DECLARE
e_highlimit EXCEPTION;
v_sal employees.salary%TYPE;
BEGIN
UPDATE employees SET salary=salary+100
WHERE employee_id=108 RETURNING salary INTO v_sal;
IF v_sal>6000 THEN
DBMS_OUTPUT.PUT_LINE('The salary is too large! ');
ROLLBACK;
END IF;
END;
用SELECT INTO 语句输出last_name是’Smith’的工资,用OTHERS异常处理器及用SQLCODE:返回当前错误代码,用SQLERRM:返回当前错误的消息文本
DECLARE
v_sal employees.salary%TYPE;
v_code NUMBER(6);
v_text VARCHAR2(200);
BEGIN
SELECT salary INTO v_sal FROM employees WHERE last_name='Smith';
DBMS_OUTPUT.PUT_LINE('salary: '||v_sal);
EXCEPTION
WHEN OTHERS THEN
v_code:=SQLCODE;
v_text:=SQLERRM;
DBMS_OUTPUT.PUT_LINE(v_code||' '||v_text);
END;
创建一个存储过程,以部门号为参数输出该部门的人数,平均工资(保留两位小数),最高工资、最低工资
create or replace
procedure proc_deptinfo(
p_deptno employees.department_id%type
)
as
p_count NUMBER;
p_avgsal employees.salary%type;
p_maxsal employees.salary%type;
p_minsal employees.salary%type;
begin
select count(*),avg(salary),max(salary), min(salary) into p_count,p_avgsal, p_maxsal, p_minsal from employees
where department_id=p_deptno;
dbms_output.put_line('部门号是:'||p_deptno||' 的部门的人数是:'||round(p_count,2)||' 部门的平均工资是:'||p_avgsal||' 最高工资是:'|| p_maxsal||' 最低工资是:'|| p_minsal);
exception
when no_data_found then
dbms_output.put_line('The department don''t exists!');
end proc_deptinfo;
-- client
set serveroutput on;
declare
p_deptno employees.department_id%type;
begin
p_deptno := &x;
proc_deptinfo(p_deptno);
end;
创建一个函数,以部门编号为参数,返回部门的平均工资
create or replace function f_getavgsal(f_dno employees.department_id%type)
return number--返回工资的数据类型
as
f_avgsal employees.salary%type;
begin
select avg(salary) into f_avgsal from employees where department_id= f_dno;
return f_avgsal;--返回工资
end f_getavgsal;
--client
set serveroutput on;
declare
p_deptno employees.department_id%type;
v_avgsal employees.salary%type;
begin
p_deptno := &x;
v_avgsal := f_getavgsal(p_deptno);
DBMS_OUTPUT.PUT_LINE('部门号是:'||p_deptno||' 的部门平均工资是:'||v_avgsal);
end;
编写PL/SQL块,根据输入的员工号将该员工的工资增加600(禁止在每天8:00-12:00之外的时间进行增加、修改、删除操作)
create or replace
trigger trg_emp
before insert or update or delete
on employees
begin
if to_char(sysdate, 'hh24:mi') not between '08:00' and '12:00'
then raise_application_error(-20005,'只能在上班时间修改');
end if;
end trg_emp;
--client
set serveroutput on;
declare
e exception;
pragma exception_init(e,-20005);
employeeId employees.employee_id%type;
begin
employeeId := &x;
update employees set salary= salary+600 where employee_id=employeeId;
dbms_output.put_line('修改成功!');
exception
when e then
dbms_output.put_line('只能在上班时间修改');
end;