--1.定义一个函数,查询某个员工的年收入createorreplacefunction f1(f_en emp.ename%type)return number
is
years number;beginselect nvl2(comm,sal+comm,sal)into years from emp where ename=f_en;return12*years;end;begin
dbms_output.put_line(f1('KING'));end;
--2.定义一个函数,记录类型作为返回类型,根据指定的部门号返回其对应的部门信息createorreplacefunction f1(f_deptno dept.deptno%type)return dept%rowtype
is
rec dept%rowtype;beginselect*into rec from dept where deptno=f_deptno;return rec;end;declare
v_dn dept.deptno%type:='&a';begin
dbms_output.put_line(f1(v_dn).deptno||' '||f1(v_dn).dname||' '||f1(v_dn).loc);end;
--3. 定义函数,部门编码作为输入参数,查询出该部门的员工总数。createorreplacefunction f1(f_dn emp.deptno%type)return number
is
c number;beginselectcount(empno)into c from emp where deptno=f_dn;return c;end;begin
dbms_output.put_line(f1(10));end;
--7. 每天12点以后,不允许修改雇员的工资和奖金createorreplacetrigger t1 before updateof sal,comm on emp for each rowdeclare
shijian varchar2(10);begin
shijian:=to_char(sysdate,'HH24');if to_number(shijian)>12then--将varchar2类型转换为
raise_application_error(-20100,'每天12点以后,不允许修改雇员的工资和奖金');endif;end;update emp set sal=40where empno=2612;
-- 8. 每一位雇员都要根据收入缴所得税,假设所得税的上缴原则为:2000以下上缴3%,2000—5000上缴8%,-- 5000以上上缴10%。现在要求建立一张新的数据表,可以记录出雇员的编号、姓名、工资、奖金和上缴所-- 得税数据,并且在每次修改雇员表中sal和comm字段后可以自动更新记录。createtable c1(
empno number(4),
ename varchar2(10),
sal number,
comm number,
tax number
);createorreplacetrigger t1 afterinsertorupdateof sal,comm on emp
declarecursor cur isselect*from emp;
rec cur%rowtype;
tax number;
ico emp.sal%type;beginopen cur;loopfetch cur into rec;exitwhen cur%notfound;
ico:=rec.sal+nvl(rec.comm,0);if ico<2000then
tax:=ico*0.03;
elsif ico between2000and5000then
tax:=ico*0.08;
elsif ico>5000then
tax:=ico*0.1;endif;insertinto c1 values(rec.empno,rec.ename,rec.sal,rec.comm,tax);endloop;end;update emp set comm=100where empno=2612;select*from c1;
--9. 写一个函数 输入一个员工名字,判断该名字在员工表中是否存在。存在返回 1,不存在返回 0createorreplacefunction f2(em emp.ename%type)return number
is
num number(2);
n number(2);beginselectcount(*)into num from emp where ename=em;if num>0then n:=1;
elsif num=0then n:=0;endif;return n;end;begin
dbms_output.put_line(f2('wan'));end;
--10. 写一个函数,传入员工编号,返回所在部门名称createorreplacefunction f1(emn emp.empno%type)returnvarcharis
dm dept.dname%type;
dn dept.deptno%type;beginselect deptno into dn from emp where empno=emn;select dname into dm from dept where deptno=dn;return dm;
exception
when no_data_found then
dbms_output.put_line('无数据');end;begin
dbms_output.put_line(f1(1111));end;