【Oracle数据库】实验-%type+%rowtype+分支语句

1、利用%type,显示输出scott.emp表中的部分数据

set serveroutput on;
declare
  emp_number  constant number(4):=7900;
  emp_name  scott.emp.ename%type;
  emp_job  scott.emp.job%type;
  emp_sal  scott.emp.sal%type;
begin
  select ename,job,sal
      into emp_name,emp_job,emp_sal
              from scott.emp where empno=emp_number;
               dbms_output.put_line('查询的员工号为'||emp_number);
               dbms_output.put_line('该员工的姓名为'||emp_name);
               dbms_output.put_line('该员工的职位为'||emp_job);
               dbms_output.put_line('该员工的工资为'||emp_sal);
end;

在这里插入图片描述
2、利用%rowtype,显示输出scott.emp表中的部分数据

declare
  emp_number  constant scott.emp.empno%type:=7900;
  one_emp  scott.emp%rowtype;
  begin
    select *
        into one_emp
            from scott.emp where empno=emp_number;
    dbms_output.put_line('查询的员工号为'||emp_number);
    dbms_output.put_line('该员工的姓名为'||one_emp.ename);
    dbms_output.put_line('该员工的职位为'||one_emp.job);
     dbms_output.put_line('该员工的工资为'||one_emp.sal);
end;

在这里插入图片描述

涉及表为scott.emp,输入一个员工号,修改该员工的工资,如果该员工为10号部门(deptno),则要求工资增加100;若为20号部门,要求工资增加150;若为30号部门,工资增加200;否则增加300。

if+%type

declare
   v_deptno scott.emp.empno%type;
   v_zj number(4);
begin
   select deptno into v_deptno
       from scott.emp where empno=&&a;
   if        v_deptno=10          then v_zj:=100;
     elsif   v_deptno=20          then v_zj:=150;
     elsif   v_deptno=30          then v_zj:=200;
     else    v_zj:=300;
   end if; 
  update scott.emp set sal=sal+v_zj  where empno=&a;
end;

在这里插入图片描述

case+%type

declare
   v_deptno scott.emp.empno%type;
   v_zj number(4);
begin
   select deptno into v_deptno
       from scott.emp where empno=&&a;
   case
     when   v_deptno=10          then v_zj:=100;
     when   v_deptno=20          then v_zj:=150;
     when   v_deptno=30          then v_zj:=200;
     else   v_zj:=300;
   end case; 
  update scott.emp set sal=sal+v_zj  where empno=&a;
end;

在这里插入图片描述

case+%rowtype

declare
   v_emp scott.emp%rowtype;
   v_zj number(4);
   v_empno scott.emp%rowtype;
begin
   select * into v_emp
       from scott.emp where empno=&&a;
   case
     when   v_emp.deptno=10          then v_zj:=100;
     when   v_emp.deptno=20          then v_zj:=150;
     when   v_emp.deptno=30          then v_zj:=200;
     else   v_zj:=300;
   end case; 
  update scott.emp set sal=sal+v_zj  where empno=&a;
end;

在这里插入图片描述

通过case语句显示每一位同学的获得学分情况。

select xh,xm,zxf,
(case
    when zxf>50 then '高'
    when zxf>=40 then '中'
    else '学分不够,需继续!'
 end)as
 获得学分情况 from xs;

在这里插入图片描述
图书借书是否过期

select empno,ename,job,hiredate,
(case
    when trunc(sysdate-HIREDATE)>360  then '过期'
    when hiredate is null then '没借书'
     else  '没过期'
    end) 
   as 是否过期 from scott.emp;

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值