plsql基础练习题

时间:2024-03-08 14:14:00

1.键盘输入一个年份,判断是否是闰年;

(能被4整除而不能被100整除或者能被100和400同时整除,满足其一即可);

方法1
  
declare
  v_year number(4):=&请输入一个4个字符的年份;
begin
  if mod(v_year,4)=0 and mod(v_year,100)<>0
     or mod(v_year,100)=0 and mod(v_year,400)=0 then
     dbms_output.put_line(\'您输入的年份是\'||v_year||\'是一个闰年\');
     else
       dbms_output.put_line(\'您输入的年份是\'||v_year||\'不是闰年\');
  end if;
end;
方法2
  
declare
  v_year number(4):=&请输入一个4个月份的年份;
begin
  if mod(v_year,4)=0 and mod(v_year,100)<>0 then
    dbms_output.put_line(\'您输入的年份是\'||v_year||\'是一个闰年\');
    elsif mod(v_year,100)=0 and mod(v_year,400)=0 then
      dbms_output.put_line(\'您输入的年份是\'||v_year||\'是一个闰年\');
       else
         dbms_output.put_line(\'您输入的年份是\'||v_year||\'不是闰年\');
  end if;
end;

2.键盘输入一个deptno,将全部门的人的信息打印出来;

方法1
  
declare
  --v_emp emp&rowtype;
  v_deptno number(2):=&请输入一个部门编号;
begin
  for v_emp in (select * from emp where deptno=v_deptno) loop
    dbms_output.put_line(rpad(v_emp.empno, 4,\' \')||\' , \'||
                         nvl(rpad(v_emp.ename,10,\' \'),\'          \')||\' , \'||
                         nvl(rpad(v_emp.job,9,\' \'),\'         \')||\' , \'||
                         nvl(rpad(v_emp.mgr,4,\' \'),\'    \')||\' , \'||
                         nvl(to_char(v_emp.hiredate,\'YYYYMMDD\'),\'        \')||\' , \'||
                         nvl(rpad(v_emp.sal,4,\' \'),\'    \')||\' , \'||
                         nvl(rpad(v_emp.comm,7,\' \'),\'       \')||\' , \'||
                         nvl(rpad(v_emp.deptno,2,\' \'),\'  \')
    );
  end loop;
end;
执行结果

键盘输入20,输出结果如下

  
7777 , S_HH%GGH   , CLERK     , 7902 ,          , 900  ,         , 20
7369 , SMITH      , CLERK     , 7902 , 19801217 , 800  ,         , 20
7566 , JONES      , MANAGER   , 7839 , 19810402 , 2975 ,         , 20
7788 , SCOTT      , ANALYST   , 7566 , 19870419 , 3000 ,         , 20
7876 , ADAMS      , CLERK     , 7788 , 19870523 , 1100 ,         , 20
7902 , FORD       , ANALYST   , 7566 , 19811203 , 3000 ,         , 20

3.键盘输入一个empno,将同部门的人的信息全部打印;

方法1
  
declare
  vno number(4):=&请输入一个员工编号;
begin
  for v_emp in (select a.* from emp a,emp b where a.deptno=b.deptno and b.empno=vno) loop
    dbms_output.put_line(rpad(v_emp.empno, 4,\' \')||\' , \'||
                         nvl(rpad(v_emp.ename,10,\' \'),\'          \')||\' , \'||
                         nvl(rpad(v_emp.job,9,\' \'),\'         \')||\' , \'||
                         nvl(rpad(v_emp.mgr,4,\' \'),\'    \')||\' , \'||
                         nvl(to_char(v_emp.hiredate,\'YYYYMMDD\'),\'        \')||\' , \'||
                         nvl(rpad(v_emp.sal,4,\' \'),\'    \')||\' , \'||
                         nvl(rpad(v_emp.comm,7,\' \'),\'       \')||\' , \'||
                         nvl(rpad(v_emp.deptno,2,\' \'),\'  \')
    );
  end loop;
end;
方法2
  
declare
  vno number(4):=&请输入一个员工编号;
begin
  for v_emp in (select * from emp where deptno=(select deptno from emp where empno=vno)) loop
    dbms_output.put_line(rpad(v_emp.empno, 4,\' \')||\' , \'||
                         nvl(rpad(v_emp.ename,10,\' \'),\'          \')||\' , \'||
                         nvl(rpad(v_emp.job,9,\' \'),\'         \')||\' , \'||
                         nvl(rpad(v_emp.mgr,4,\' \'),\'    \')||\' , \'||
                         nvl(to_char(v_emp.hiredate,\'YYYYMMDD\'),\'        \')||\' , \'||
                         nvl(rpad(v_emp.sal,4,\' \'),\'    \')||\' , \'||
                         nvl(rpad(v_emp.comm,7,\' \'),\'       \')||\' , \'||
                         nvl(rpad(v_emp.deptno,2,\' \'),\'  \')
    );
  end loop;
end;
执行结果

键盘输入7566,结果如下

  
7777 , S_HH%GGH   , CLERK     , 7902 ,          , 900  ,         , 20
7369 , SMITH      , CLERK     , 7902 , 19801217 , 800  ,         , 20
7566 , JONES      , MANAGER   , 7839 , 19810402 , 2975 ,         , 20
7788 , SCOTT      , ANALYST   , 7566 , 19870419 , 3000 ,         , 20
7876 , ADAMS      , CLERK     , 7788 , 19870523 , 1100 ,         , 20
7902 , FORD       , ANALYST   , 7566 , 19811203 , 3000 ,         , 20

4.键盘介入三个值,并按照从大到小依次打印;

方法1
  
declare
  n1 number(10):=&请输入第一个值;
  n2 number(10):=&请输入第二个值;
  n3 number(10):=&请输入第三个值;
begin
  if n1>=n2 and n2>=n3 then
    dbms_output.put_line(n1||n2||n3);
    elsif n1>=n3 and n3>=n2 then
      dbms_output.put_line(n1||n3||n2);
      elsif n2>=n1 and n1>=n3 then
        dbms_output.put_line(n2||n1||n3);
        elsif n2>=n3 and n3>=n1 then
          dbms_output.put_line(n2||n3||n1);
          elsif n3>=n1 and n1>=n2 then
            dbms_output.put_line(n3||n1||n2);
            elsif n3>=n2 and n2>=n1 then
              dbms_output.put_line(n3||n2||n1);
              else
                dbms_output.put_line(n1||n2||n3);
  end if;
end;
方法2
  
declare
  a number(5) := &请输入第一个数字;
  b number(5) := &请输入第二个数字;
  c number(5) := &请输入第三个数字;

i number(5);
j number(5);
k number(5);

begin
select greatest(a, b, c) into i from dual;
select least(a, b, c) into k from dual;
select (a + b + c - i - k) into j from dual;
dbms_output.put_line(i || \',\' || j || \',\' || k);
end;

5.某公司要根据雇员的职位来加薪,公司决定按下列加薪结构处理:

方法1
  
declare
  v_name varchar2(50):=upper(\'&输入名字\');
  v_job varchar2(50);
begin
  select job into v_job from emp where ename=v_name;
  if v_job = upper(\'clerk\') then
    update emp set sal=sal+500 where ename=v_name;
    elsif v_job = upper(\'salseman\') then
      update emp set sal=sal+1000 where ename=v_name;
      elsif v_job = upper(\'analyst\') then
        update emp set sal=sal+1500 where ename=v_name;
        elsif v_job = upper(\'otherwise\') then
          update emp set sal=sal+2000 where ename=v_name;
  end if;
  commit;
end;

6.计算下面数, 当末项小于0.001时的部分和。

1/(1*2)+1/(2*3)+1/(3*4)+…+1/(n*(n+1))+ ……

for方法 结果:.9696969698
  
declare
v_num number(20,10):=0;
v_num1 number(20,10):=0;
begin
  for i in 1..100000 loop
      v_num:=1/(i*(i+1))+v_num;
      v_num1:=1/(i*(i+1));
  if v_num1<0.001 then
    dbms_output.put_line(v_num);
    exit;
   end if;

end loop;
end;

loop方法 结果:总和是: .969696969696969696969696969698, n的值是: 32
  
declare
  i number(10):=1;
  s number(38,30):=0;
begin
  loop
    s:=s+(1/(i*(i+1)));
    if 1/(i*(i+1))<0.001 then
      exit;
    end if;
    i:=i+1;
  end loop;
  dbms_output.put_line(\'总和是: \'||s||\', n的值是: \'||i);
end;
while方法 结果: .969696969696969696969696969698 .969696969696969696969696969698
  
declare
  i number(10):=1;
  s number(38,30):=0;
begin
  while s<1 loop
    s:=(1/(i*(i+1)))+s;
    if (1/(i*(i+1)))<0.001 then
      dbms_output.put_line(s);
      exit;
    end if;
    i:=i+1;
  end loop;
  dbms_output.put_line(s);
end;

7.计算s=12+23+…+N*(N+1),当N=50的值

for方法 结果:44200
  
declare
  v_num number(6):=0;
begin
  for i in 1..51 loop
    v_num:=i*(i+1)+v_num;
    if i=50 then
      dbms_output.put_line(v_num);
      exit;
    end if;
  end loop;
end;
loop方法 结果:44200
  
declare
  s number(10) := 0;
  i number(2) := 1;
begin
  loop
    s := s + i*(i + 1);
    if i = 50 then
      exit;
    end if;
    i := i + 1;
  end loop;
  dbms_output.put_line(s);
end;
while方法 44200
  
declare
  i number(10):=1;
  s number(10):=0;

begin
while i<=50 loop
s:=(i*(i+1))+s;
i:=i+1;
end loop;
dbms_output.put_line(s);
end;

8.编程序求满足不等式 1+32+52+…+N^2>2000的最小N值

loop方法 结果: 2300 23
  
declare
  i number(10) := -1;
  s number(20) := 0;
begin
  loop
    s := s + power(i + 2, 2);
    i := i + 2;
    if s > 2000 then
      exit;
    end if;
  end loop;
  dbms_output.put_line(s);
  dbms_output.put_line(i);
end;
while方法 结果: 2300 23
  
declare
  i number(10):=-1;
  s number(10):=0;
begin
  while s<=2000 loop
    s := s+power(i+2,2);
    i := i+2;
  end loop;
  dbms_output.put_line(s);
  dbms_output.put_line(i);
end;
for方法 结果: 2300 23
  
declare
  s number(10):=0;
  i number(10):=-1;
begin
  for j in 1..50 loop
    s:=s+power(i+2,2);
    i:=i+2;
    if s>2000 then
      exit;
    end if;  
  end loop;
  dbms_output.put_line(s);
  dbms_output.put_line(i);
end;

9.两重循环,计算S=1!+2!+…+10!

while方法 结果: 4037913
  
declare
  v_num number(15):=1;
  v_nums number(15):=0;
  i number(2):=1;
begin
  while i<=10 loop
    v_num:=i*v_num;
    v_nums:=v_num+v_nums;
    i:=i+1;
  end loop;
  dbms_output.put_line(v_nums);
end;
loop方法 结果: 4037913
  
declare
  v_num number(15):=1;
  v_nums number(15):=0;
  i number(2):=1;
begin
  loop
    v_num:=i*v_num;
    v_nums:=v_num+v_nums;
    if i=10 then
      dbms_output.put_line(v_nums);
      exit;
    end if;
    i:=i+1;
  end loop;
end;
for方法 结果: 4037913
  
declare
  v_num number(15):=1;
  v_num1 number(15):=0;
begin
  for i in 1..10 loop
    v_num:=i*v_num;
    v_num1:=v_num+v_num1;
    if i=10 then
      dbms_output.put_line(v_num1);
    end if;
  end loop;
end;