【Oracle】练习与作业4

mac2025-07-16  5

1)从emp表中查询指定员工的工资,并判断该员工的工资是否低于2000。如果条件成立,那么该员工的工资增加200。(单分支if)

set serveroutput on; select empno 员工编号,sal 工资 from scott.emp; declare x scott.emp.empno%type:=&员工编号; y scott.emp.sal%type; begin sys.dbms_output.put_line(‘本题要求:从emp表中查询指定员工的工资,并判断该员工的工资是否低于2000。如果条件成立,那么该员工的工资增加200’); select sal into y from scott.emp where empno = x; sys.dbms_output.put_line(‘更新前:员工编号为:’||x||‘的员工的工资为:’||y||‘元’); if y <2000 then update scott.emp set sal = sal + 200 where empno = x; end if; select sal into y from scott.emp where empno = x; sys.dbms_output.put_line(‘更新后:员工编号为:’||x||‘的员工的工资为:’||y||‘元’); end; /

保存成q1.txt

2)根据给定的职工编号从emp表中查询该职工的奖金(comm字段的值),如果奖金的值不为0,那么将表中该职工的奖金增加200,否则(即comm为0或null)将该职工的奖金设置为100。(双分支if) 本题"奖金的值不为0"的判断,因为comm有空值null字段,需要使用nvl函数. nvl(表达式1,表达式2):如果表达式1为空值,nvl返回值为表达式2的值,否则返回表达式1的值。该函数的目的就是将null值转换成具体值.

set serveroutput on; select empno 员工编号,comm 工资 from scott.emp; declare x scott.emp.empno%type:=&员工编号; y scott.emp.comm%type; begin select comm into y from scott.emp where empno = x; sys.dbms_output.put_line(‘本题要求:根据给定的职工编号从emp表中查询该职工的奖金(comm字段的值),如果奖金的值不为0,那么将表中该职工的奖金增加200,否则(即comm为0或null)将该职工的奖金设置为100。(双分支if) 本题"奖金的值不为0"的判断,因为comm有空值null字段,需要使用nvl函数.’); sys.dbms_output.put_line(‘更新前:员工编号为:’||x||‘的员工的奖金为:’||nvl(y,0)||‘元’); if nvl(y,0) !=0 then update scott.emp set comm = comm + 200 where empno = x; else update scott.emp set comm = 100 where empno = x; end if; select comm into y from scott.emp where empno = x; sys.dbms_output.put_line(‘更新后:员工编号为:’||x||‘的员工的奖金为:’||y||‘元’); end; /

保存成q2.txt

3)从emp表中查询指定员工的工作,并根据工作来修改他的工资。工作是PRESIDENT工资加1000元,工作是MANAGER工资加500元,工作是CLERK工资加200元,其余工作工资加100元。(多分支if)

set serveroutput on; select empno 员工编号,job 工作,sal 工资 from scott.emp; declare x scott.emp.empno%type:=&员工编号; y scott.emp.job%type; z scott.emp.sal%type; begin select job,sal into y , z from scott.emp where empno = x; sys.dbms_output.put_line(‘本题要求:从emp表中查询指定员工的工作,并根据工作来修改他的工资。工作是PRESIDENT工资加1000元,工作是MANAGER工资加500元,工作是CLERK工资加200元,其余工作工资加100元。(多分支if)’); sys.dbms_output.put_line(‘更新前:员工编号为:’||x||‘的员工的工资为:’||z||‘元’); if y = ‘PRESIDENT’ then update scott.emp set sal=sal+100 where empno = x; elsif y = ‘MANAGER’ then update scott.emp set sal=sal+500 where empno = x; elsif y = ‘CLERK’ then update scott.emp set sal=sal+200 where empno = x; else update scott.emp set sal=sal+100 where empno = x; end if; select sal into z from scott.emp where empno = x; sys.dbms_output.put_line(‘他的工作为:’||y||’, 因此,更新后他的工资为:’||z||‘元’); end; /

保存成q3.txt

4)修改emp表中指定部门的职工的奖金。10号部门奖金改为100元,20号部门奖金改为80元,30号部门奖金改为50元,其余情况,显示’部门不存在’。(case)

set serveroutput on; select deptno 部门编号 from scott.dept; declare x scott.dept.deptno%type := &部门编号; y scott.emp.comm%type; begin sys.dbms_output.put_line(‘修改emp表中指定部门的职工的奖金。10号部门奖金改为100元,20号部门奖金改为80元,30号部门奖金改为50元,其余情况,显示部门不存在(case)’); case x when 10 then y := 100; when 20 then y := 80; when 30 then y := 50; else sys.dbms_output.put_line(‘部门不存在’); end case; if y >0 then update scott.emp set comm = y where deptno = x; end if; sys.dbms_output.put_line(‘部门编号为’||x||‘的职工的奖金为’||y||‘元’); end; /

保存成q4.txt

5)根据员工的工资,修改emp表中的奖金值。工资小于1000元,奖金为100元;工资小于2000元,奖金为80元;工资小于6000元,奖金为50元;其他情况,奖金为30元。(case)

set serveroutput on; sys.dbms_output.put_line('更新前: '); select empno 员工编号,ename 姓名, sal 工资,comm 奖金 from scott.emp; declare emp_info scott.emp%rowtype; var_sal scott.emp.sal%type; var_comm scott.emp.comm%type; cursor cur_emp is select * into emp_info from scott.emp; begin open cur_emp; fetch cur_emp into emp_info ; sys.dbms_output.put_line('更新后: ‘); sys.dbms_output.put_line(‘员工编号 姓名 工资 奖金 ‘); sys.dbms_output.put_line(’_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _’); while cur_emp%found loop case when nvl(emp_info.sal,0) < 1000 then var_comm:=100; when emp_info.sal < 2000 then var_comm:=80; when emp_info.sal < 6000 then var_comm:=50; else var_comm:=30; end case; update scott.emp set comm = var_comm where sal = emp_info.sal; sys.dbms_output.put_line(’ ‘||emp_info.empno||’ ‘||emp_info.ename||’ ‘||emp_info.sal||’ '||emp_info.comm); fetch cur_emp into emp_info; end loop; close cur_emp; end; /

保存成q5.txt

6)用loop语句求某个数的阶乘。

set serveroutput on; declare x int := &n; y int :=1; begin sys.dbms_output.put_line(‘本题要求:用loop语句求某个数的阶乘。’); loop y := y*x; x :=x-1; exit when x=0; end loop; sys.dbms_output.put_line(‘它的阶乘是:’||y); end; /

保存成q6.txt

7)用while语句求某个数的阶乘。

set serveroutput on; declare x int := &n; y int :=1; begin sys.dbms_output.put_line(‘本题要求:用while语句求某个数的阶乘。’); while x!=0 loop y := y*x; x :=x-1; end loop; sys.dbms_output.put_line(‘它的阶乘是:’||y); end; /

保存成q7.txt

8)用for语句求某个数的阶乘。

set serveroutput on; declare x int := &n; y int :=1; z constant int := x; begin sys.dbms_output.put_line(‘本题要求:用for语句求某个数的阶乘。’); for x in reverse 1…z loop y := y*x; end loop; sys.dbms_output.put_line(‘它的阶乘是:’||y); end; /

保存成q8.txt

最新回复(0)