miércoles, 9 de mayo de 2012

Solucion Control Cursores



set serveroutput on

Problema 1

declare
  cursor Uno is select first_name,last_name,salary from employees order by last_name;
  recEmpleado Uno%rowtype;
  contador integer := 0;
begin
  open Uno;  
      loop
        fetch Uno into recEmpleado;
        exit when Uno%NOTFOUND;
        contador := contador + 1;
        dbms_output.put_line('Nombre: '||recEmpleado.first_name||' Apellido: '||recEmpleado.last_name||' Salario: '||recEmpleado.salary);
      end loop;
  close Uno;
  dbms_output.put_line('Total de empleados: '||contador);
  exception
    when others then
    dbms_output.put_line('Error');
end;

Problema 2






set serveroutput on
declare
  cursor Uno is select first_name,last_name,salary,hire_date from employees order by last_name;
  recEmpleado Uno%rowtype;
  contador integer := 0;
  edad integer;
  msg varchar2(10);
begin
  open Uno;
  fetch Uno into recEmpleado;
      while Uno%FOUND loop      
        contador := contador + 1;
        edad := round((sysdate - recEmpleado.hire_date)/365);
        if edad <= 25 then
          msg := 'Junior';
        elsif edad > 25 and edad < 40 then
          msg := 'Senior';
        else
          msg := 'Master';
        end if;
        fetch Uno into recEmpleado;
        dbms_output.put_line('Nombre: '||recEmpleado.first_name||' Apellido: '||recEmpleado.last_name||' Salario: '||recEmpleado.salary||' Mensaje : '||msg);
      end loop;
  close Uno;
  dbms_output.put_line('Total empleados '||contador);
  exception
    when others then
    dbms_output.put_line('Error');
end;





desc departments
set serveroutput on
declare
  cursor Uno is
  select employees.first_name,employees.last_name,employees.salary,departments.DEPARTMENT_NAME
  from employees, departments
  where departments.department_id = employees.department_id
  order by DEPARTMENT_NAME;
  recEmpleado Uno%rowtype;
  contador integer := 0;
  edad integer;
  msg varchar2(10);
begin
  for recEmp in Uno loop
   dbms_output.put_line('Nombre:  '||recEmp.first_name);
  end loop;

  exception
    when others then
    dbms_output.put_line('Error');
end;




No hay comentarios:

Publicar un comentario