PL/SQL上机作业

技术分享

Declare
   i number;
   j number;
   mark number;
   cout number:=0;
Begin
   for i in 1..1000 Loop
      mark :=0;
     for j in 2..i/2 Loop
       if (i mod j = 0 )Then
         mark:=1;
         exit;
       end if;
      end Loop;
      
      if (mark = 0) Then
         cout:=cout+1;
         dbms_output.put(i||‘,‘);
        if mod(cout,5)=0 Then
         dbms_output.new_line();
        end if;
      end if;
    end Loop;
    dbms_output.new_line();
    dbms_output.put_line(‘合计共有‘||cout||‘个‘);
End;
/

  技术分享

declare 
  v_high_sal employees.salary%TYPE;
  v_high_job jobs.job_title%TYPE;
  v_high_count number;
  v_low_sal employees.salary%TYPE;
  v_low_job jobs.job_title%TYPE;
  v_low_count number;
  
begin
  select *
  into v_high_job,v_high_sal,v_high_count
  from (select job_title,avg(salary),count(*) from employees,jobs where employees.job_id=jobs.job_id 
  group by job_title
  order by avg(salary) desc )
  where rownum=1;
  
  select *
  into v_low_job,v_low_sal,v_low_count
  from (select job_title,avg(salary),count(*) from employees,jobs where employees.job_id=jobs.job_id 
  group by job_title
  order by avg(salary))
  where rownum=1;
  
  dbms_output.put_line(平均工资最高的职位是||v_high_job||,||平均最高工资是||v_high_sal||,||共有||v_high_count||人担任此职||;);
  dbms_output.put_line(平均工资最低的职位是||v_low_job||,||平均最高工资是||v_low_sal||,||共有||v_low_count||人担任此职||;);
  dbms_output.put_line(平均工资处于两级的职位平均工资的差别为||(v_high_sal-v_low_sal)||;);
end;
/

技术分享

declare
   cursor curTest is
       select job_title,min_salary,max_salary,count(*)
       from jobs, employees
       where jobs.job_id=employees.job_id
       group by job_title,min_salary,max_salary;
   type rec_type is record
       (
           job_name jobs.job_title%type,
           min_sal  jobs.min_salary%type,
           max_sal  jobs.max_salary%type,
           cout     number
       );
    v_JobTitle rec_type;
    times number(3,1);
    e_NOT_SIMILAR EXCEPTION;
begin 
  open curTest;
  fetch curTest into v_JobTitle;
  Loop
    begin
    exit when curTest%NOTFOUND;
      times := (v_JobTitle.max_sal/v_JObTitle.min_sal);
      if(times<=1.5) then 
          dbms_output.put_line(职位||v_JobTitle.job_name||的最低工资为||v_JObTitle.min_sal||,||最高工资为||v_JobTitle.max_sal||,||两者的差别为||times||||,||供职此职位的人有||v_JobTitle.cout||,||工资变化区间正常);
      else
         raise e_NOT_SIMILAR;
      end if;
     EXCEPTION
        when e_NOT_SIMILAR then
           dbms_output.put_line(职位||v_JobTitle.job_name||工资变化区间过大); 
    end;
    fetch curTest into v_JobTitle;
  end Loop;
  close curTest;
  
end;
/

技术分享

with dept_job as
(select dname,
      case job when CLERK then sal end CLERK,
      case job when SALESMAN then sal end SALESMAN,
      case job when PRESIDENT then sal end PRESIDENT, 
      case job when MANAGER then sal end MANAGER, 
      case job when ANALYST then sal end ANALYST
 from emp join dept on emp.deptno=dept.deptno)
 select dname,NVL(to_char(avg(CLERK)),) CLERK,NVL(to_char(avg(SALESMAN)),) SALESMAN,NVL(to_char(avg(PRESIDENT)),) PRESIDENT,
        NVL(to_char(avg(MANAGER)),) MANGER,NVL(to_char(avg(ANALYST)),) ANALYST
 from dept_job
 group by dname;
 

技术分享

select dname,CLERK,SALESMAN,PRESIDENT,MANAGER,ANALYST
from (select dname,sal,job from emp join dept on
      emp.deptno=dept.deptno) dept_job
pivot
(avg(sal)
  for job in(CLERK as CLERK,SALESMAN as SALESMAN,
             PRESIDENT as PRESIDENT,MANAGER as MANAGER,ANALYST as ANALYST)
)p
      

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。