Oracle学习笔记(五)(二)

2014-11-24 16:20:17 · 作者: · 浏览: 2
create or replace function tax_ning(
v_sal number)
return number
is
v_result number;
begin
if (v_sal < 1000) then
v_result := 0;
elsif (v_sal < 2000) then
v_result := v_sal * 0.01;
elsif (v_sal < 3000) then
v_result := v_sal * 0.02;
else
v_result := v_sal * 0.04;
end if;
return v_result;
end;
--测试函数的使用
select ename, sal, tax_ning
(sal) from emp;
--函数输入参数:deptno,输出参数:部门人数
create or replace function emp_count(
v_deptno emp.deptno%type)
return number
is
v_count number;
begin
select count(*) into v_count
from emp where deptno = v_deptno;
return v_count;
end;
--测试
select emp_count(10) from dual;
--函数:必须返回数据,在sql语句中生效
--过程:可以不返回数据,可以独立调用
create or replace procedure myproc(
v_deptno emp.deptno%type)
is
v_count number;
begin
select count(*) into v_count
from emp where deptno = v_deptno;
dbms_output.put_line(v_count);
end;
--测试过程的方式
exec myproc(20)
--有输出参数的过程
create or replace procedure calcu_emp(
v_deptno in number(2),
v_sum_sal out number(7,2),
v_avg_sal out emp.sal%type)
is
begin
select sum(sal), avg(sal)
into v_sum_sal, v_avg_sal
from emp
where deptno = v_deptno;
end;
--测试有输出参数的过程
declare
v_sum number;
v_avg number;
begin
calcu_emp(10, v_sum, v_avg);
dbms_output.put_line(v_sum);
dbms_output.put_line(v_avg);
end;
--功能:修改员工薪水
--输入参数:员工编码,新的薪水值.
--如果员工的职位不是MANAGER或者PRESIDENT,且薪水高于15000,则报错.
--否则,修改指定员工的薪水为指定值.
create or replace procedure changesal(
v_empno emp.empno%type,
v_sal emp.sal%type)
is
v_job emp.job%type;
begin
select job into v_job
from emp where empno = v_empno;
if (v_job not in ('MANAGER','PRESIDENT') and v_sal > 15000) then
dbms_output.put_line('too many sal');
else
update emp set sal = v_sal
where empno = v_empno;
commit;
end if;
exception
when others then
dbms_output.put_line('some error!');
end;
--测试方式
exec changesal(7698, 20000);