我是靠谱客的博主 安静书本,这篇文章主要介绍函数练习题,现在分享给大家,希望可以做个参考。

  1. 定义一个函数,用于计算emp表中某个部门的平均工资。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
create or replace function davg(dno emp.deptno%type) return number is f_avg emp.sal%type; begin select avg(sal) into f_avg from emp where deptno=dno; return f_avg; end; declare begin dbms_output.put_line(davg(10)); end;
  1. 写一个函数,传入员工编号,返回所在部门名称
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
create or replace function redname(eno emp.empno%type) return varchar2 is dname dept.dname%type; begin select dname into dname from dept,emp where dept.deptno=emp.deptno and empno=eno; return dname; end; declare begin dbms_output.put_line(redname(7788)); end;
  1. 写一个函数,可以查询某个员工的年收入,包括奖金
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
create or replace function asal(eno emp.empno%type) return emp.sal%type is years emp.sal%type; begin select (nvl2(comm,sal+comm,sal))*12 into years from emp where empno=eno; --nvl()函数:实现空值的转换,其语法格式如下:nvl(a,b); 如果a为null,则返回b;反之,返回a; --nvl2()函数:nvl2(a,b,c); 如果a为null,则返回c;若不为null,则返回b return years; end; --select * from emp; declare v_empno emp.empno%type:=&aa; begin dbms_output.put_line(v_empno||'的年收入是'||asal(v_empno)); end;
  1. 定义函数,输入部门编号,查询出该部门的员工总数。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
create or replace function ac(dno emp.deptno%type) return number is dc number; begin select count(empno) into dc from emp where deptno=dno; return dc; end; declare v_dno emp.deptno%type:=&aa; begin dbms_output.put_line(v_dno||'部门的人数是'||ac(v_dno)); end;
  1. 定义函数,使用记录类型作为返回类型,根据指定的部门号返回其对应的部门信息
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
create or replace function dm(dno dept.deptno%type) return dept%rowtype is cursor mycur is select * from dept where deptno=dno; dp dept%rowtype; begin open mycur; loop fetch mycur into dp; exit when mycur%notfound; return dp; end loop; close mycur; end; declare v_deptno dept.deptno%type:=&aa; r1 dept%rowtype; begin r1:=dm(v_deptno); dbms_output.put_line(r1.deptno||' '||r1.dname||' '||r1.loc); end;

最后

以上就是安静书本最近收集整理的关于函数练习题的全部内容,更多相关函数练习题内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(94)

评论列表共有 0 条评论

立即
投稿
返回
顶部