时间:2021-07-01 10:21:17 帮助过:27人阅读
declare
--声明变量
v_w number(8,2):=&三角形的底边;
v_h number(8,2):=&三角形的高;
v_s number(8,2);
begin
--计算面积
v_s:=(v_w*v_h)/2;
--控制台输出
dbms_output.put_line(‘三角形的面积:‘||v_s);
end;
declare
--声明变量
v_empno number(8,2):=&员工编号;
v_count number(8,2);
v_avg number(8,2);
begin
case v_empno
when 10 then
dbms_output.put_line(‘部门名称为:技术部‘);
when 20 then
dbms_output.put_line(‘部门名称为:销售部‘);
when 30 then
dbms_output.put_line(‘部门名称为:公关部‘);
when 40 then
dbms_output.put_line(‘部门名称为:开发部‘);
else
dbms_output.put_line(‘输入错误‘);
end case;
?
select count(*) into v_count from emp e where deptno=v_empno;
dbms_output.put_line(‘总人数为:‘||v_count);
select avg(sal+nvl(comm,0)) into v_avg from emp
where deptno=v_empno;
dbms_output.put_line(‘平均工资为:‘||v_avg);
?
end;
declare
type emp_table_type is table of emp%rowtype
index by binary_integer;
v_emp emp_table_type;
v_i number(8):=1;
begin
--这里用到了bulk collect into (批量效率的读取游标数据)
select * bulk collect into v_emp from emp ;
loop
if v_i>v_emp.count then
exit;
end if;
dbms_output.put_line(
‘员工编号:‘||v_emp(v_i).empno
||‘员工姓名:‘||v_emp(v_i).ename
||‘工资:‘||v_emp(v_i).sal
||‘部门编号:‘||v_emp(v_i).deptno
);
v_i:=v_i+1;
end loop;
?
end;
declare
type emp_table_type is table of emp%rowtype
index by binary_integer;
v_emp emp_table_type;
begin
select * bulk collect into v_emp from emp where sal>(
select avg(sal) from emp where deptno=&no)and
deptno=&no;
for v_i in 1..v_emp.count
loop
dbms_output.put_line(‘员工编号‘||v_emp(v_i).empno||
‘员工姓名‘||v_emp(v_i).ename ||
‘上级领导‘||v_emp(v_i).job ||
‘领导编号‘||v_emp(v_i).mgr ||
‘入职日期‘|| v_emp(v_i).hiredate ||
‘工资‘||v_emp(v_i).sal ||
‘奖金‘|| v_emp(v_i).comm ||
‘职位编号‘||v_emp(v_i).deptno);
end loop;
?
end;
declare
type emp_table_type is table of emp%rowtype
index by binary_integer;
v_emp emp_table_type;
v_i number(8):=1;
begin
select * bulk collect into v_emp from emp;
while v_i<v_emp.count
loop
dbms_output.put_line(
‘员工编号‘||v_emp(v_i).empno||
‘员工姓名‘||v_emp(v_i).ename||
‘领导姓名‘||v_emp(v_i).job||
‘部门编号‘||v_emp(v_i).deptno);
v_i:=v_i+1;
end loop;
end;
declare
v_comm emp.comm%type;
v_yyyy number(8);
v_empno emp.empno%type:=&员工编号;
begin
--三个查询,先查询出当前日期和员工入职日期,然后计算出两者的相差
--months_between 计算两个时间段相差的月份
select months_between(
(select sysdate from dual),
(select hiredate from emp where empno=v_empno))
into v_yyyy from dual;
--查询出工资员工
select comm into v_comm from emp where empno=v_empno;
--将计算后的时间换算为年
v_yyyy:=v_yyyy/12;
--判断
if v_yyyy>=6 then
if v_comm is null then
update emp set comm=(2000) where empno=v_empno;
else
update emp set comm=(v_comm+2000) where empno=v_empno;
end if;
dbms_output.put_line(‘发放奖金2000元‘);
elsif v_yyyy<6 then
if v_comm is null then
update emp set comm=(1500) where empno=v_empno;
else
update emp set comm=(v_comm+1500) where empno=v_empno;
end if;
dbms_output.put_line(‘发放奖金1500元‘);
else
dbms_output.put_line(‘输入错误‘);
end if;
end;
declare
v_i number(8):=1;
v_m varchar2(1000):=‘‘;
begin
loop
if v_i>100 then
exit;
end if;
v_m:=v_m||v_i||‘ ‘;
v_i:=v_i+1;
end loop;
dbms_output.put_line(v_m);
?
end;
declare
v_i number(8):=1;
v_m varchar2(1000):=‘‘;
begin
while v_i<=100
loop
v_m:=v_m||v_i||‘ ‘;
v_i:=v_i+1;
end loop;
dbms_output.put_line(v_m);
?
end;
declare v_m varchar2(1000):=‘‘; begin for v_i in 1..100 loop v_m:=v_m||v_i||‘ ‘; end loop; dbms_output.put_line(v_m); end;
declare v_i number(8); v_j number(8):=1; v_n number(8):=0; begin for v_i in 1..100 loop while v_j<=v_i loop if mod(v_i,v_j)=0 then v_n:=v_n+1; end if; v_j:=v_j+1; end loop; v_j:=0; if v_n=2 then dbms_output.put_line(v_i); end if; v_n:=0; end loop; end;
pl/sql练习案例
标签:修改 当前日期 har while循环 声明 when 批量 exp ble