declare
v_sal number(10);
v_job varchar2(20);
begin
select sal,job into v_sal,v_job from emp where ename='SMITH';
dbms_output.put_line(v_sal||','||v_job);
end;
declare
v_dname varchar2(20);
v_loc varchar2(20);
begin
select dname,loc into v_dname,v_loc from dept where deptno='&部门号';
dbms_output.put_line(v_dname||','||v_loc);
end;
declare
v_sal emp.sal%type;
v_comm emp.comm%type;
begin
select sal,comm into v_sal,v_comm from emp where empno='&雇员号';
dbms_output.put_line(v_sal+nvl(v_comm,0));
end;
declare
v_emp emp%rowtype;
begin
select * into v_emp from emp where empno='&雇员号';
dbms_output.put_line(v_emp.sal+nvl(v_emp.comm,0));
end;
declare
v_n1 number(5);
v_n2 number(5);
begin
v_n1:=&请输入第一个数字;
v_n2:=&请输入第二个数字;
if v_n1>v_n2
then dbms_output.put_line(v_n1);
else
dbms_output.put_line(v_n2);
end if;
end;
declare
v_n1 number(5);
v_n2 number(5);
v_n3 number(5);
begin
v_n1:=&请输入第一个数字;
v_n2:=&请输入第二个数字;
v_n3:=&请输入第三个数字;
if v_n1>v_n2 and v_n1>v_n3
then
if v_n2>v_n3 then
dbms_output.put_line(v_n1||' '||v_n2||' '||v_n3);
else
dbms_output.put_line(v_n1||' '||v_n3||' '||v_n2);
end if;
elsif v_n2>v_n1 and v_n2>v_n3
then
if v_n1>v_n3 then
dbms_output.put_line(v_n2||' '||v_n1||' '||v_n3);
else
dbms_output.put_line(v_n2||' '||v_n3||' '||v_n1);
end if;
elsif v_n3>v_n2 and v_n3>v_n1
then
if v_n2>v_n1 then
dbms_output.put_line(v_n3||' '||v_n2||' '||v_n1);
else
dbms_output.put_line(v_n3||' '||v_n1||' '||v_n2);
end if;
end if;
end;
declare
v_year number(5);
begin
v_year:=&请输入年份;
if (v_year mod 4 =0 and v_year mod 100 !=0) or v_year mod 400=0
then dbms_output.put_line(v_year||'是闰年');
else
dbms_output.put_line(v_year||'不是闰年');
end if;
end;
declare
v_n1 number(10);
v_n2 number(10);
begin
v_n1:=&请输入第一个数;
v_n2:=&请输入第二个数;
if v_n2=0
then dbms_output.put_line('除数不能为0');
else
dbms_output.put_line(v_n1/v_n2);
end if;
end;
declare
v_w number(5);
v_h number(5);
v_bmi number(5);
begin
v_w:=&体重;
v_h:=&身高;
v_bmi:=v_w/(power(v_h,2));
if v_bmi<=18.4
then dbms_output.put_line('偏瘦 <= 18.4');
elsif v_bmi>=18.5 and v_bmi<=23.9
then dbms_output.put_line('正常 18.5 ~ 23.9');
elsif v_bmi>=24.0 and v_bmi<=27.9
then dbms_output.put_line('过重 24.0 ~ 27.9');
else
dbms_output.put_line('肥胖 >= 28.0');
end if;
end;
declare
v_n number(5);
begin
v_n:=&请输入一个数;
if v_n mod 2=0 then
dbms_output.put_line(v_n||'是偶数');
else
dbms_output.put_line(v_n||'是奇数');
end if;
end;
declare
v_n number(5);
begin
v_n:=&请输入一个数;
if sqrt(v_n)>(v_n/2) then
dbms_output.put_line(v_n||'的平方根大于'||v_n||'除以2的值');
elsif sqrt(v_n)=(v_n/2) then
dbms_output.put_line(v_n||'的平方根等于'||v_n||'除以2的值');
else
dbms_output.put_line(v_n||'的平方根小于'||v_n||'除以2的值');
end if;
end;
Designation Raise
clerk 500
salseman 1000
analyst 1500
otherwise 2000
编写一个程序块,接受一个雇员名,从emp表中实现上述加薪处理
法一:
declare
v_name varchar2(10):='&请输入一个雇员名';
v_job varchar2(10);
v_sal number(5);
begin
select job into v_job from emp where ename=v_name;
case v_job
when 'CLERK' then v_sal:=500;
when 'SALESMAN' then v_sal:=1000;
when 'ANALYST' then v_sal:=1500;
else v_sal:=2000;
end case;
update emp set sal=sal+v_sal where ename=v_name;
end;
法二:
declare
v_name emp.ename%type:='&name';
v_sal emp.sal%type;
v_job emp.job%type;
begin
select lower(job) into v_job from emp where ename=upper(v_name);
if v_job='clerk' then v_sal:=500;
elsif v_job='salesman' then v_sal:=1000;
elsif v_job='analyst' then v_sal:=1500;
else v_sal:=2000;
end if;
update emp set sal=sal+v_sal where ename=upper(v_name);
end;
begin
for i in (select ename from emp) loop
dbms_output.put_line(i.ename);
end loop;
end;
法一:
begin
for i in (select ename from (select ename,rownum from emp
where rownum<=5)) loop
dbms_output.put_line(i.ename);
end loop;
end;
法二:
declare
n number(5):=1;
begin
for i in (select ename from emp) loop
dbms_output.put_line(i.ename);
exit when n=5;
n:=n+1;
end loop;
end;
法三:
begin
for i in (select ename,rownum r from emp) loop
if i.r<=5 then
dbms_output.put_line(i.ename);
end if;
end loop;
end;
法一:
declare
v_n number(20,10):=&请输入一个数;
v_sum number(20,10):=0;
begin
for i in 1..v_n loop
v_sum:=v_sum+1/(i*(i+1));
exit when 1/(i*(i+1))<0.001;
end loop;
dbms_output.put_line(v_sum);
end;
法二:
declare
v_sum number(20,10):=0;
i number(5):=1;
begin
loop
v_sum:=v_sum+1/(i*(i+1));
exit when 1/(i*(i+1))<0.001;
i:=i+1;
end loop;
dbms_output.put_line(v_sum);
end;
declare
v_n number(10):=&请输入一个数;
v_sum number(10):=0;
begin
for i in 1..v_n loop
v_sum:=v_sum+i*(i+1);
end loop;
dbms_output.put_line(v_sum);
end;
法一:
declare
v_sal emp.sal%type;
begin
for v_sal in (select sal from emp
where ename like 'A%' or ename like 'S%') loop
v_sal.sal:=v_sal.sal*1.1;
dbms_output.put_line(v_sal.sal);
end loop;
end;
法二:
declare
v_name varchar2(10);
begin
for v_name in (select ename from emp
where ename like 'A%' or ename like 'S%') loop
update emp set sal=sal*1.1 where ename=v_name.ename;
dbms_output.put_line(v_name.ename);
end loop;
end;
declare
v_n1 number(10):=2; v_n2 number(10);
v_n3 number(10); v_sum number(10):=1;
begin
for v_n1 in 1..9 loop
v_n3:=1;
for v_n2 in 2..(v_n1+1) loop
v_n3:=v_n3*v_n2;
end loop;
v_sum:=v_sum++v_n3;
end loop;
dbms_output.put_line(v_sum);
end;
declare
v_n number(10);
v_i number(10):=1;
v_sum number(10):=0;
begin
while v_sum<2000 loop
v_sum:=v_sum+power(v_i,2);
v_i:=v_i+2;
end loop;
dbms_output.put_line(v_i-2);
end;
declare
n number(10):=0;
begin
for i in (select empno from emp where sal<3000) loop
update emp set sal=sal+400 where empno=i.empno;
n:=n+1;
end loop;
dbms_output.put_line('增加工资人数:'||n);
dbms_output.put_line('增加工资数量:'||n*400);
end;
法一:
begin
for i in(select * from(select ename,deptno,sal,row_number() over(order by sal desc) as r
from emp)where r<=5)loop
dbms_output.put_line(i.ename||','||i.deptno||','||i.sal);
end loop;
end;
法二:
begin
for i in (select e.*,row_number() over(order by sal desc) r from emp e) loop
if i.r<=5 then
dbms_output.put_line(i.ename||','||i.deptno||','||i.sal);
end if;
end loop;
end;