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;