1,写函数和过程,输入三角形三个表的长度。在控制台打印三角形的面积。
过程:
1 create or replace procedure pro_area(v_num1 number,
2 v_num2 number,v_num3 number)
3 is
4 v_area number(10,2);
5 begin
6 v_area:=sqrt((v_num1+v_num2+v_num3)/2*(((v_num1+v_num2+
7 v_num3)/2)-v_num1)*(((v_num1+v_num2+
8 v_num3)/2)-v_num2)*(((v_num1+v_num2+
9 v_num3)/2)-v_num3));
10 dbms_output.put_line(v_area);
11 end;
12 call pro_area(3,4,5);
函数:
1 create or replace function fun_area(v_num1 number,
2 v_num2 number,v_num3 number) return number
3 is
4 v_area number(10,2);
5 begin
6 v_area:=sqrt((v_num1+v_num2+v_num3)/2*(((v_num1+v_num2+
7 v_num3)/2)-v_num1)*(((v_num1+v_num2+
8 v_num3)/2)-v_num2)*(((v_num1+v_num2+
9 v_num3)/2)-v_num3));
10 return v_area;
11 end;
12
13 declare
14 v_area number(10,2);
15 begin
16 v_area:=fun_area(4.5,5.5,8);
17 dbms_output.put_line(v_area);
18 end;
2,写一个过程,输入部门编号,在控制台打印这个部门的名称,总人数,平均工资(基本工资+奖金)
1 create or replace procedure pro_emp(v_deptno number)
2 is
3 v_dname dept.dname%type;
4 v_total dept.deptno%type;
5 v_avg emp.sal%type;
6 begin
7 select dname,count(e.empno),sum(sal+nvl(comm,0))/count(e.empno)
8 into v_dname,v_total,v_avg from emp e inner join dept d on e.deptno=d.deptno
9 where e.deptno=v_deptno group by d.dname;
10 dbms_output.put_line('部门编号为'||v_deptno||'的部门'||v_dname||
11 '中有'||v_total||'人,这个部门的平均工资为:'||v_avg);
12 end;
13
14 call pro_emp(20);
3,编写一个存储过程,输入一个员工编号,输出该员工的员工编号,员工姓名,部门名称。
1 create or replace procedure pro_dept(v_eemmpno number)
2 is
3 v_ename emp.ename%type;
4 v_empno emp.empno%type;
5 v_dname dept.dname%type;
6 begin
7 select e.ename,e.empno,d.dname into
8 v_ename,v_empno,v_dname from emp e inner join
9 dept d on e.deptno=d.deptno and e.empno=v_eemmpno;
10 dbms_output.put_line('员工编号:'||v_empno||
11 '员工姓名:'||v_ename||'部门名称'||v_dname);
12 end;
13
14 call pro_dept(7839);
4,编写一个存储过程,输出所有员工及其部门领导的姓名、员工号及部门号。
1 create or replace procedure pro_allemp
2 is
3 type emp_record_type is record(
4 v_ename emp.ename%type,
5 v_lname emp.ename%type,
6 v_empno emp.empno%type,
7 v_deptno emp.deptno%type);
8 type emp_table_type is table of emp_record_type
9 index by binary_integer;
10 v_emp emp_table_type;
11 begin
12 select ename,nvl((select e2.ename from emp e2 where
13 e1.mgr=e2.empno),'无'),empno,deptno bulk collect
14 into v_emp from emp e1;
15 for v_i in 1..v_emp.count
16 loop
17 dbms_output.put_line('员工姓名:'||v_emp(v_i).v_ename||',领导姓名:'||
18 v_emp(v_i).v_lname||',员工号:'||v_emp(v_i).v_empno||',部门编号:'||
19 v_emp(v_i).v_deptno);
20 end loop;
21 end;
22
23 call pro_allemp();