/*开启控制台输出*/
set serveroutput on;
set verify off;
declare
v_pi number(3,2):=3.14;
v_r number(3,2):=2;
v_area number(4,2);
begin
select v_r*v_r*v_pi into v_area from dual;
dbms_output.put_line('面积是:'||v_area);
end;
/**与数据库类型相对应*/
declare
v_eno emp3.ename%type;/*和数据库相对应*/
begin
select ename into v_eno from emp3 where emp3.empno = &empno;
dbms_output.put_line('用户名是:'||v_eno);
exception
when no_data_found then dbms_output.put_line('输入错误');
end;
/*record 类型*/
declare
type dept_record_type is record(
v_deptno dept3.deptno%type,
v_dname dept3.dname%type,
v_loc dept3.loc%type
);
dept_record dept_record_type;
begin
select * into dept_record from dept3 where deptno = &no;
dbms_output.put_line('部门编号:'||dept_record.v_deptno);
exception
when no_data_found then
dbms_output.put_line('输入有误');
end;
/*table 类型*/
declare
type dept_table is table of dept3%rowtype index by binary_integer;
dept_t dept_table;
begin
select * into dept_t(0) from dept3 where deptno =&no;
dbms_output.put_line('部门名称'||dept_t(0).dname);
end;
/*判断数据相等与否,直接使用 dept_t(0).dname='ACCOUNTING' */
/* if----elsif else */
declare
type dept_table is table of dept3%rowtype index by binary_integer;
dept_t dept_table;
begin
select * into dept_t(0) from dept3 where deptno =&no;
if dept_t(0).dname='ACCOUNTING' then dbms_output.put_line('ACCOUNTING');
elsif
dept_t(0).dname='RESEARCH' then dbms_output.put_line('RESEARCH');
elsif
dept_t(0).dname='SALES' then dbms_output.put_line('SALES');
elsif
dept_t(0).dname='OPERATIONS' then dbms_output.put_line('OPERATIONS');
else
dbms_output.put_line('输入有误');
end if;
end;
/* case -- when */
declare
v_dept emp%rowtype ;
begin
select * into v_dept from emp3 where emp3.empno= &empno;
case v_dept.ename
when 'WARD' then dbms_output.put_line('ward');
when 'FORD' then dbms_output.put_line('FORD');
else dbms_output.put_line('不存在该编号');
end case;
end;