miércoles, 20 de junio de 2012

Triggers y Sequence


create sequence seqLiquidacion
start with 1
increment by 1
nomaxvalue;

create trigger liquidacionIncr
before insert on liquidacion
for each row
begin
select seqLiquidacion.nextval into :new.idliquidacion from dual;
end;

miércoles, 6 de junio de 2012

Package Luis en clases


create or replace package pkgLuis is
  function cantidadEmpleados(pDepto IN employees.department_id%type) return number;
end pkgLuis;

create or replace
package body pkgLuis is
  FUNCTION cantidadEmpleados(
 pDepto IN employees.department_id%type
)
  RETURN NUMBER AS
 
  totalEmpleados integer;
  BEGIN
    select count(*) into totalEmpleados
    from Employees
    where department_id = 90;
    RETURN totalEmpleados;
  END cantidadEmpleados;
end pkgLuis;

select * from employees where department_id = 90;

set serveroutput on
begin
  dbms_output.put_line('Cantidad :'||pkgLuis.cantidadEmpleados(90));
end;
set serveroutput on
declare
  cantidad number;
begin
  select pkgLuis.cantidadEmpleados(90) into cantidad from dual;
  dbms_output.put_line('Cantidad :'||cantidad);
end;

select pkgLuis.cantidadEmpleados(90) from dual;

package - 06-06-2012


create or replace package pkg_uno is
function CUENTAEMPLEADOS (pDepto IN employees.department_id%type) return number;
end pkg_uno;

create or replace package body pkg_uno is
 FUNCTION CUENTAEMPLEADOS (pDepto IN employees.department_id%type)

 RETURN NUMBER AS
totalEmpleados integer;
BEGIN
  select count(*) into totalEmpleados
  from Employees
  where department_id = pDepto;
  RETURN totalEmpleados;
END CUENTAEMPLEADOS;
end pkg_uno;



    select pkg_uno.CUENTAEMPLEADOS(90) from dual;

Clase Trigger


create table Autor(
idAutor int,
nombre varchar(30),
constraint Pk_idAutor primary key (idAutor)  
);

begin
insert into Autor values(7);
insert into Autor VALUES(1);
insert into Autor VALUES(2);
insert into Autor VALUES(3);
insert into Autor VALUES(4);
insert into Autor VALUES(5);
end;

create table Libro(
IdLibro integer,
IdAutor integer,
constraint Pk_idLibro primary key (IdLibro),
constraint Fk_IdAutor foreign key (IdAutor) references Autor
);

begin
insert into Libro values(1, 1);
insert into Libro values(2, 1);
insert into Libro values(3, 2);
insert into Libro values(4, 3);
end;

create sequence seqAutor
start with 1 increment by 10
nocache
nocycle;

create or replace trigger MiPrimerTrigger
after delete on Autor
for each row
begin
  DELETE FROM Libro
  WHERE IdAutor = :old.IdAutor;
end;

begin
delete from Autor
where idAutor = 1;
end;

select * from Libro;

sábado, 2 de junio de 2012

Trigger


create or replace trigger IngresoDetalleCompra
after insert on DetalleCompra
for each row
begin
  update Producto set StockActual = StockActual + :new.Cantidad where idProducto = :new.idProducto;
end;

sábado, 19 de mayo de 2012

Actualizar departamento empleado con procedure


Procedimiento almacenado que alctualiza la tabla empleados

create or replace procedure actualizarDepartamento(pempleadoId in char,pdepartamentoId in char)
is
total integer;
begin
  total := 0;
  select count(*) into total from empleado;
  if total > 0 then
    update empleado set numoficina = pdepartamentoId where numempleado = pempleadoId;
  end if;
end actualizarDepartamento;


begin
  actualizarDepartamento('SL21','B006');
  dbms_output.put_line('Actualizado');
end;

Repaso funciones


Funcion para contar cuantos empleados por departamento hay.


create or replace function empleadosPorDepartamento(departamento number)
return number
is
resultado number;
begin
  select count(*) as contador into resultado  from employees where department_id = departamento;
  return resultado;
end empleadosPorDepartamento;


set serveroutput on
declare
  resultado number;
begin
  resultado := empleadosPorDepartamento(50);
  dbms_output.put_line('El resultado es: '||resultado);
end;

Forma para mostrar los datos y la cantidad de empleados llamando a la funcion creada anteriormente.

select employee_id,department_id,empleadosPorDepartamento(50) from employees