plsql basic exercises

1. The keyboard input a year, it is determined whether or not a leap year;

(Divisible by four, or can not be divisible by 100 divided by 400 and 100 simultaneously, one can be satisfied);

method 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;
Method 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. Enter a keyboard deptno, the people of sector-wide information printed;

method 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;
Results of the

Keyboard input 20, the output results are as follows

  
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. Enter a keyboard empno, the man with all the information department of printing;

method 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;
Method 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;
Results of the

Keyboard input 7566, the following results

  
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. Keyboard involved three values, and descending order according to the print;

method 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;
Method 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. A company should pay to employees based on the job, the company decided to deal with the following pay structure:

method 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. Calculate the number of the following, when the time is less than 0.001 part and the last item.

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

for methods Results: .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 method Results: The sum is: .969696969696969696969696969698, the value of n is: 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 method Results: .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. Calculate. 1 = S 2 + 2 . 3 + ... + N * (N +. 1), when the value of N = 50

for methods Results: 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 method Results: 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 method 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. programmed find the minimum value of N satisfies the inequality 1 + 3 ^ 2 + 5 ^ 2 + ... + N ^ 2> 2000's

loop method Results: 230,023
  
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;
  

  

Guess you like

Origin www.cnblogs.com/inmeditation/p/12025045.html