jueves, 30 de junio de 2011

  1. http://pdb002.blogspot.com/search?updated-max=2011-05-02T16%3A21%3A00-07%3A00&max-results=7
create or replace package pkgMipaquete is
function suma (p1 number, p2 number) return number;
function resta (p1 number, p2 number) return number;
function multiplica(p1 number, p2 number) return number;
function divide(p1 number, p2 number) return number;
end pkgMipaquete;
create or replace package BODY pkgMipaquete is
procedure imprime(pvalor number) is
BEGIN
dbms_output.put_line('Resultado : ' || pvalor);
end;
function suma(p1 number, p2 number) return number is
begin
imprime(p1+p2);
return(p1+p2);
end suma;
function resta(p1 number, p2 number) return number is
begin
imprime(p1-p2);
return(p1-p2);
end resta;
function multiplica(p1 number, p2 number) return number is
begin
imprime(p1*p2);
return(p1*p2);
end multiplica;
function divide(p1 number, p2 number) return number is
begin
imprime(p1/p2);
return(p1/p2);
EXCEPTION
when zero_divide then
dbms_output.put_line ('Cuack : Division por cero');
return 0;
end divide;
end pkgMipaquete;
declare
r number(10);
begin
r:= pkgMipaquete.suma(2,3);
end

jueves, 16 de junio de 2011

Corregir

1.       Realizar una función que reciba una  cadena ej:  'ARCA' y la muestre por consola repetida n veces Ej: 7 veces.


CREATE OR REPLACE function RepiteCadena(pstr1 varchar2,pn number)
return varchar2 as
 str2 varchar(100) = null;
 i numeric = 1;
 begin
 loop
   str2 = concat(str2,pstr1);
   i = i + 1;
   exit when i >= pn;
 end loop;
 return str2;
 end;
2. Crear un procedimiento con un cursor que actualice el atributo TotEmpleado

Declare
cursor miCursor
is

select e.numoficina, count(e.numempleado) as Empleados
from oficina p, empleado e
where p.numoficina = e.numoficina
group by e.numoficina;

vnumoficina oficina.numoficina%type;
vtotempleado oficina.numoficina%type;

begin
open micursor;
loop
fetch micursor into vnumoficina, vtotempleado;
exit when micursor%notfound;
update oficina
set totempleado = vtotempleado
where numoficina = vnumoficina;
end loop;
close micursor;
End;


select * from oficina

lunes, 13 de junio de 2011

Desarrollar 4 funciones ,
Desarrollar 4 trigger,
Desarrolar 4 cursores

Enunciado , implementación y el llamado.
http://www.scribd.com/doc/49946651/27/Funciones-en-PL-SQL

2. Crear otra función que al ingresar un número entero......entregue por consola el numero elevado al cubo.



CREATE OR REPLACE FUNCTION elevarAlCubo(n1 Numeric)
RETURN numeric
is
BEGIN
return power(n1,2);
END;

SELECT elevarAlCubo(2) from dual;
1. Crear una función que reciba 2 números y los sume...y el resultado lo muestre por la consola.


CREATE OR REPLACE FUNCTION sumar(n1 Numeric,n2 numeric)
RETURN numeric
is
BEGIN
return n1+n2;
END;


SELECT sumar(4,2) from dual;

jueves, 2 de junio de 2011

PARA QUE SIRVE LOS CURSORES,

CUANDO SE HACE UNA CONSULTA Y ENTREGA UNA CANTIDAD DE VALORES ,
LOS CURSORES SIRVEN PARA HACER UN RECORRIDO DE LA TABLA.

NO DEJAR REGISTRO DE ACTUALIZACIONES

CREATE or replace TRIGGER trLogRenta
after update of salario on empleado
for each row
BEGIN
if :new.salario <> :old.salario then

 insert into RENTALOG VALUES (SEQRENTALOG.nextval,
 user,
 sysdate,
 :old.salario,
 :new.salario,
 :old.numempleado);
end if;
END;

REGISTRO 2 , con la misma renta

REGISTRO

CAMBIAR DE RENTA A UN 20% +

UPDATE EMPLEADO
SET SALARIO = SALARIO * 1.2
WHERE NUMEMPLEADO = 'SL21'

SENTENCIAS

INSERT (..)
:NEW
DELETE (..)
:OLD

VERIFICAR ERROR EN EL TRIGGER

SHOW ERR TRIGGER trLogRenta

INSTRUCCION

crear trigger que cada vez que modifique el sueldo de un empleado deje registro.

crear trigger que cada vez que modifique el sueldo de un empleado deje registro.

create table RENTALOG (
IDRENTALOG NUMERIC primery key,
USUARIO VARCHAR(20),
FECHA DATE,
SUELDOANTIGUO FLOAT,
SUELDONUEVO FLOAT,
NUMEMPLEADO VARCHAR(20)
);

CREATE SEQUENCE SEQRENTALOG
INCREMENT BY 2
START WITH 10

CREATE or replace TRIGGER trLogRenta
after update of salario on empleado
for each row
BEGIN
insert into RENTALOG VALUES (SEQRENTALOG.nextval,
user,
sysdate,
:old.salario,
:new.salario,
:old.numempleado);
END;
Declare
cursor miCursor
is
select e.numoficina, count(p.numpropiedad) as Propiedades
from propiedad p, empleado e
where p.numempleado = e.numempleado
group by e.numoficina;

vnumoficina oficina.numoficina%type;
vtotpropiedad oficina.TOTPROPIEDAD%type;

begin
open micursor;
loop
fetch micursor into vnumoficina, vtotpropiedad;
exit when micursor%notfound;
 update oficina
 set TOTPROPIEDAD = vtotpropiedad
 where numoficina = vnumoficina;
end loop;
close micursor;
End;

Create or replace trigger miTrigger
after  delete or insert or update of numempleado on propiedad
for each row
declare
vOldOfiEmpleado char(4);
vNewOfiEmpleado char(4);
begin
  if updating then
 select numoficina into vOldOfiEmpleado
 from empleado
 where numempleado = :old.numempleado;
 select numoficina into vNewOfiEmpleado
 from empleado
 where numempleado = :new.numempleado;
 if vOldOfiEmpleado <> vNewOfiEmpleado then
update oficina
set totPropiedad = totPropiedad - 1
where numOficina = vOldOfiEmpleado;

update oficina
set totPropiedad = totPropiedad + 1
where numOficina = vNewofiEmpleado;
 end if;
 else
    if deleting then
 select numoficina into vOldOfiEmpleado
      from empleado
      where numempleado = :old.numempleado;

 update oficina
 set totPropiedad = totPropiedad - 1
 where numOficina = vOldOfiEmpleado;
else
select numoficina into vNewOfiEmpleado
        from empleado
        where numempleado = :new.numempleado;
update oficina
   set totPropiedad = totPropiedad + 1
   where numOficina = vNewofiEmpleado;
end if;
end if;
end;


Chequeamos los

select e.numempleado, p.numpropiedad
from empleado e, propiedad p
where numoficina = 'B005'
and e.numempleado = p.numempleado

Buscamos algun empleado que no tenga propiedades

Vamos a cambiar la asignacion de la propiedad "PG16" que la tiene el
empleado "SL22" de la oficina "B005" con 5 propiedades y la asignamos al empleado "SG37" de la oficina "B006" que no
tiene propiedades.

Hacemos el siguiente update

update propiedad
set numempleado = 'SG37'
where numpropiedad = 'PG16';

Eliminando la propiedad "PL94" de la oficina "B005" que le quedan 4 propiedades

delete from propiedad
where numpropiedad = 'PL94'

Ahora insertamos una nueva propiedad al empleado "SG37" de la Oficina "B006"

insert into PROPIEDAD values('P007','Nueva','Aberdeem','AB7 5SU','Casa','6','650','C046','SG37');

Finalmente la Oficina "B006" deberia quedar con DOS (2) Propiedades