1、创建一个过程raise_sal,传递4个参数,部门编号(默认是
20)、雇佣日期(默认日期2006-01-01)、薪水、影响行的数
量,前三个参数为输入参数,最后的参数为输出参数。
根据输入的部门编号和雇佣日期(雇员的雇佣日期小于参数的
雇佣日期)信息更新对应雇员的薪水,在原有的基础上增加传
入的薪水信息,并将更新的行数传递到输出参数中。
create or replace procedure Raise_sal(
dptno IN NUMBER DEFAULT 20,
hire IN DATE DEFAULT '01-1月-2006',
sa number,-------增加的工资
cou OUT NUMBER
)
IS
CURSOR cur IS
SELECT * FROM emp
WHERE deptno = dptno;
rec emp%ROWTYPE;
var NUMBER;
BEGIN
FOR rec IN cur LOOP
IF rec.hiredate < '01-1月-2006' AND rec.deptno = dptno THEN
cou := cur%Rowcount;
UPDATE emp SET sal = rec.sal + sa
WHERE deptno = rec.deptno;
END IF;
END LOOP;
COMMIT;
end Raise_sal;
2、创建另一个过程test,调用raise_sal,传递不同的值到
raise_sal过程中,并打印更新的行数信息。
create or replace procedure test_raise_sal(
dptno IN NUMBER ,
hire IN DATE ,
sa number,-------增加的工资
ss IN OUT NUMBER
)
IS
begin
raise_sal(dptno,hire,sa,ss);
dbms_output.put_line(ss);
end test_raise_sal;
3..汇总每个部门每个职位员工的最高及平均工资,包括显示部门名称
和职位名称,将数据存储到Test表中,并将工资最高的部门和职位,
以及最高工资用OUT参数输出。要求使用存储过程
test提前建好
create or replace procedure test_maxmin
IS
CURSOR cur IS
SELECT department_name,job_id,MAX(salary) mm,AVG(salary) aa
FROM employees e,departments d
WHERE e.department_id = d.department_id
GROUP BY department_name,job_id
ORDER BY department_name;
rec cur%ROWTYPE;
begin
FOR rec IN cur LOOP
INSERT INTO testt VALUES(rec.department_name,
rec.job_id,
rec.mm,
rec.aa);
END LOOP;
COMMIT;
end test_maxmin;
4、创建函数,根据输入的参数(员工代码)值,返回对应的
员工姓名
create or replace function test_name(
no Number
)
RETURN varchar2
is
var varchar2(15) ;
begin
SELECT employees.first_name
INTO var
FROM employees
WHERE employee_id = no;
return(var);
end test_name;
--------------------SELECT test_name(198) FROM dual测试
5、创建一个函数,根据输入的参数(部门代码)值,返回对
应的部门员工的最高薪水;
create or replace function test_depart(
no NUMBER
)
return Number
is
var number;
begin
SELECT MAX(salary)
INTO var
FROM employees
WHERE department_id = no;
return(var);
end test_depart;
---------------------------------测试
SELECT test_depart(20) FROM dual
SELECT * FROM employees WHERE department_id = 20
创建一个存储过程,查询dept表的所有部门信息,调用该函
数,当函数返回的薪水大于4000时,产生异常,在异常部分
显示‘某某部门的薪水太高了'。
create or replace procedure test_heighsal
IS
CURSOR cur IS
SELECT * FROM departments;
rec departments%ROWTYPE;
begin
FOR rec IN cur LOOP---------必须用nvl,因为有的部门没有员工
IF NVL(test_depart(rec.department_id) ,0)< 1500000 THEN
dbms_output.put_line(rec.department_id||','||
rec.department_name||','||
rec.manager_id||','||
rec.location_id||','||
NVL(test_depart(rec.department_id) ,0));
ELSE
dbms_output.put_line(rec.department_id
||'工资太高了');
EXIT;
END IF;
END LOOP;
end test_heighsal;
-----------------------测试
declare
begin
test_heighsal();
end;
6.根据员工的信息,要根据员工的工资,计算每月应缴纳税额,将信息存储到tax表中
如果每月《= 3000 0.50%
如果每月《= 3000 6000
0.80%
3000*0.5%+(工资-
3000)*0.8%
如果每月《= 6000 10000
1.00%
如果每月《= 12000 15000
1.20%
如果每月《= 15000
2.00%
create or replace procedure test_tax
IS
CURSOR cur IS
SELECT employee_id,salary
FROM employees;
rec cur%ROWTYPE;
taxx NUMBER;
begin
FOR rec IN cur LOOP
IF nvl(rec.salary,0)<=3000 THEN
taxx := nvl(rec.salary,0)*0.05;
ELSIF nvl(rec.salary,0)>3000 AND nvl(rec.salary,0)<=6000 THEN
taxx :=(nvl(rec.salary,0)-3000)*0.008*+3000*0.005;
ELSIF nvl(rec.salary,0)>6000 AND nvl(rec.salary,0)<=10000 THEN
taxx :=(nvl(rec.salary,0)-10000)*0.01+3000*0.008+3000*0.005;
ELSIF nvl(rec.salary,0)>10000 AND nvl(rec.salary,0)<12000 THEN
taxx :=(nvl(rec.salary,0)-12000)*0.012+3000*0.008+3000*0.005+4000*0.01;
ELSIF nvl(rec.salary,0)>12000 THEN
taxx :=(nvl(rec.salary,0)-12000)*0.015+3000*0.008+3000*0.005+4000*0.01;
END IF;
INSERT INTO tax
VALUES(rec.employee_id,rec.salary,taxx);
END LOOP;
COMMIT;
end test_tax;