--计算收入为3000-4000的所得税总额
select sum((sal-3000)*0.3+300) into xx from emp where sal >3000 and sal<=4000; sum_xx:=sum_xx+xx;
--计算收入为4000以上的所得税总额
select sum((sal-4000)*0.4+600) into xx from emp where sal >4000; sum_xx:=sum_xx+xx;
dbms_output.put_line(sum_xx); end;
7. *(可选做,难题)假设有个表如myEMP,未建立主键,含有多条记录重复(列值完全
相同),试编制一个PL/SQL,将多余的重复记录删除。 实验六、
1. 用外部变量,实现两个PL/SQL程序间的数据交换。 SQL> variable a1 number; SQL> begin 2 :a1:=1000; 3 end; 4 /
PL/SQL 过程已成功完成。
SQL> begin
2 dbms_output.put_line(:a1); 3 end; 4 / 1000
PL/SQL 过程已成功完成。
2. 插入myEMP表中的数据记录,考虑可能出现的例外,并提示。 主要的例外提示:唯一性索引值重复DUP_VAL_ON_INDEX
3. 删除myDEPT表中的数据记录一条,考虑例外情况,并提示。 主要的例外提示:违反完整约束条件
4. 将下列PL/SQL改为FOR游标 declare
cursor cur_myemp is select * from emp; r emp%rowtype; begin
open cur_myemp;
fetch cur_myemp into r; while cur_myemp%found loop
dbms_output.put_line(r.ename); fetch cur_myemp into r; end loop;
close cur_myemp; end;
5. 工资级别的表salgrade,列出各工资级别的人数。(用游标来完成) declare v1 number;
cursor cur1 is select * from salgrade; begin
for c1 in cur1 loop
select count(*) into v1 from emp where sal between c1.losal and c1.hisal; dbms_output.put_line('grade'||c1.grade||' '||v1); end loop; end;
实验七、
1. 在myEMP表中增加一个字段,字段名为EMPPASS,类型为可变长字符。
2. 建立一个存储过程,用于操作用户登录的校验,登录需要使用EMPNO和EMPPASS,
并需要提示登录中的错误,如是EMPNO不存在,还是EMPNO存在而是EMPPASS错误等。
create or replace procedure p_login( in_empno in emp.empno%type, in_emppass in emp.emppass%type, out_code out number, out_desc out varchar2) is
x1 emp.ename%type; x2 number; begin
select ename into x1 from emp where empno=in_empno;
select count(*) into x2 from emp where empno=in_empno and emppass=in_emppass; if x2=1 then out_code:=0; out_desc:=x1; else
out_code:=2;
out_desc:=?用户登陆密码错误!?; end if; exception
when NO_DATA_FOUND then out_code:=1;
out_desc:=?该用户号存在!?;
when TOO_MANY_ROWS then out_code:=3;
out_desc:=?该用户号有重复值!?; when others then out_code:=100;
out_desc:=?其他错误!?; end;
3. 建立一个存储过程,实现myEMP表中指定雇员的EMPPASS字段的修改,修改前必须
进行EMPPASS旧值的核对。
CREATE OR REPLACE PROCEDURE P_CHANGEPASS( IN_EMPNO IN EMP.EMPNO%TYPE, IN_OLDPASS IN EMP.EMPPASS%TYPE, IN_NEWPASS IN EMP.EMPPASS%TYPE, OUT_CODE OUT NUMBER, OUT_DESC OUT VARCHAR2) IS
X1 NUMBER; BEGIN
SELECT COUNT(*) INTO X1 FROM EMP WHERE EMPNO=IN_EMPNO AND EMPPASS=IN_OLDPASS; IF X1=1 THEN
update emp set emppass=in_newpass where empno=in_empno; commit;
OUT_CODE:=0;
OUT_DESC:=?修改口令成功?; ELSE
OUT_CODE:=1;
OUT_DESC:=?修改口令不成功?; END IF; exception
when others then out_code:=100;
out_desc:=?其他错误?; END;
4. 建立一个函数,输入一个雇员号,返回该雇员的所在同一部门的最高级别上司姓名。
create or replace function f_leader(
in_empno in emp.empno%type) return varchar2 is
v1 number; v2 number;
v3 emp.ename%type; v4 emp.deptno%type;
begin
v1:=in_empno; v3:='未找到';
select deptno into v4 from emp where empno=v1; loop
select mgr into v2 from emp where empno=v1;
select ename into v3 from emp where empno=v2 and deptno=v4; v1:=v2; end loop; exception
when others then return v3; end;
5. 试用上题函数,实现各雇员的同一部门最高级别上司的SELECT查询。 select f_leader(7521) from dual;
6. *编写实验五中第六题,关于各雇员工资的所得税计算函数
实验八、
1. 建立一个触发器,当myEMP表中部门号存在时,该部门不允许删除。 create or replace trigger dept_line_delete before delete on dept for each row declare v1 number; begin
select count(*) into v1 from emp where deptno=:old.deptno; if v1>=1 then RAISE_APPLICATION_ERROR(-20000,?错误?); end if; end;
实验九、
1. 建立一个示例包emp_mgmt中,新增一个修改雇员所在部门的过程。 create or replace package emp_mgmt as procedure change_dept(
in_newdept in emp.deptno%type, out_code out number, out_desc out varchar2);
mgmt_empno emp.empno%type; procedure mgmt_login(
in_empno in emp.empno%type, in_emppass in emp.emppass%type, out_code out number, out_desc out varchar2);
end;
create or replace package body emp_mgmt as procedure change_dept(
in_newdept in emp.deptno%type, out_code out number, out_desc out varchar2) is begin
update emp set deptno=in_newdept where empno=mgmt_empno; commit; out_code:=0; out_desc:=?ok?; end;
procedure mgmt_login(
in_empno in emp.empno%type, in_emppass in emp.emppass%type, out_code out number, out_desc out varchar2) is begin
--登陆过程见实验七第2题 mgmt_empno:=in_empno; out_code:=0; out_desc:=?ok?; end; end;
2. 假设myEMP表中有口令字段password,试在包emp_mgmt中建立一个登录的过程,并
将登录成功的雇员号存入包变量。 见前一题
3. 示例包emp_mgmt中,将remove_emp操作设限,只有本部门经理操作才能删除本部门
雇员记录,只有公司头头PRESIDENT才能删除部门经理的雇员记录。 --
procedure remove_emp(
remove_empno emp.empno%type, out_code number, out_desc varchar2) is
x emp.job%type; y number; begin
select job,deptno into x,y from emp where empno=mgmt_empno;
if x=?PRESIDENT? then
delete from emp where empno=remove_empno and job=?MANAGER?; else
delete from emp where empno=remove_empno and deptno=y and x=?MANAGER?; end if
if sql%found then out_code:=0; out_desc:=?ok?; else
out_code:=1;
out_desc:=?未删除记录?; end if; commit; end;
4. *用DELPHI+ORACLE实现上题的软件功能。
实验十
1. 编写一段PL/SQL,利用系统工具包,实现对SERVER端数据文件D:\\DATA\\A.TXT的
读取输出至缓冲区。
2. 编写一个存储过程,就myEMP表,输入参数为字段名和匹配值(字符型),对符合匹配
条件的工资加100。
3. 编写一个存储过程,输入参数为一个表名,通过存储过程处理将该表删除DROP,并返
回是否成功的信息。
实验十一
1. 以雇员作为对象类型,试根据myEMP表结构设计其属性,方法主要有雇员更换部门、
更换工种、MAP排序的定义。
2. 编制一个雇员类型的对象表myOBJ_EMP。 3. 添加对象表myOBJ_EMP的数据10条。 4. 试对对象表排序输出。
5. 给对象表中部门号为20的记录的工资增加10%。 6. 显示每个雇员所在的雇员名、部门名称。