Buenos noches!
1. Datos inicialos:
Código SQL:
Ver originalCREATE TABLE Prestamo(
idLector NUMERIC(10) NOT NULL,
idLibro NUMERIC(10) NOT NULL,
FechaPrestamo DATE NOT NULL,
FechaDev DATE NULL,
FechaRealDev DATE NULL,
Mora NUMBER(19,4) NULL,
estado NUMBER(1) NULL
);
INSERT INTO Prestamo VALUES (1,1,to_date('09-06-2016','DD-MM-YYYY'),to_date('12-06-2016','DD-MM-YYYY'),'','',0);
INSERT INTO Prestamo VALUES (1,2,to_date('09-06-2016','DD-MM-YYYY'),to_date('12-06-2016','DD-MM-YYYY'),'','',0);
INSERT INTO Prestamo VALUES (1,3,to_date('09-06-2016','DD-MM-YYYY'),to_date('12-06-2016','DD-MM-YYYY'),'','',0);
SQL> SELECT * FROM prestamo;
IDLECTOR IDLIBRO FECHAPRES FECHADEV FECHAREAL MORA ESTADO
---------- ---------- --------- --------- --------- ---------- ----------
1 1 09-JUN-16 12-JUN-16 0
1 2 09-JUN-16 12-JUN-16 0
1 3 09-JUN-16 12-JUN-16 0
SQL>
2. Realizo triggeros:
Código SQL:
Ver originalCREATE OR REPLACE package Test_Package AS
TYPE TTmpRec IS TABLE OF INTEGER INDEX BY Binary_integer;
TYPE TTmpTabla IS TABLE OF TTmpRec INDEX BY binary_integer;
TempTabla TTmpTabla;
Latch BOOLEAN:=FALSE;
PROCEDURE Update_Data;
END;
/
CREATE OR REPLACE package body Test_Package AS
i INTEGER;
j INTEGER;
PROCEDURE Update_Data AS
BEGIN
latch := TRUE;
IF test_Package.TempTabla.Count>0 THEN
i:=test_Package.TempTabla.FIRST;
while i IS NOT NULL loop
j:=test_Package.TempTabla(i).FIRST;
while j IS NOT NULL loop
UPDATE Prestamo SET Mora=test_Package.TempTabla(i)(j) WHERE idLector=i AND idLibro=j;
j:=test_Package.TempTabla(i).NEXT(j);
END loop;
i:=test_Package.TempTabla.NEXT(i);
END loop;
END IF;
latch := FALSE;
exception WHEN others THEN
latch := FALSE;
raise;
latch := FALSE;
END;
END;
/
CREATE OR REPLACE TRIGGER TR_CALCULARMORA_TMP
AFTER UPDATE
ON Prestamo
FOR EACH ROW
DECLARE
dias INTEGER;
BEGIN
DIAS := TRUNC(SYSDATE) - :NEW.FECHADEV;
IF dias > 3 THEN
Test_Package.TempTabla(:OLD.idLector)(:OLD.idLibro):=(dias-3)*5;
END IF;
END;
/
CREATE OR REPLACE TRIGGER TR_CALCULARMORA
After INSERT OR UPDATE OR DELETE ON Prestamo
BEGIN
IF NOT test_Package.Latch THEN
Test_Package.Update_Data;
END IF;
END;
/
CREATE OR REPLACE TRIGGER Init_TEST
BEFORE INSERT OR UPDATE OR DELETE ON Prestamo
BEGIN
IF NOT Test_Package.latch THEN
Test_Package.TempTabla.DELETE();
END IF;
END;
/
3. Resultado:
Código BASH:
Ver originalSQL> select * from prestamo;
IDLECTOR IDLIBRO FECHAPRES FECHADEV FECHAREAL MORA ESTADO
---------- ---------- --------- --------- --------- ---------- ----------
1 1 09-JUN-16 12-JUN-16 0
1 2 09-JUN-16 12-JUN-16 0
1 3 09-JUN-16 12-JUN-16 0
SQL> UPDATE Prestamo SET FECHADEV=to_date('10-06-2016','DD-MM-YYYY') WHERE idLector=1 ;
3 rows updated.
SQL> commit;
Commit complete.
SQL> select * from prestamo;
IDLECTOR IDLIBRO FECHAPRES FECHADEV FECHAREAL MORA ESTADO
---------- ---------- --------- --------- --------- ---------- ----------
1 1 09-JUN-16 10-JUN-16 10 0
1 2 09-JUN-16 10-JUN-16 10 0
1 3 09-JUN-16 10-JUN-16 10 0
SQL> select to_char(sysdate,'DD-MM-YYYY HH24:MI:SS') from dual;
TO_CHAR(SYSDATE,'DD
-------------------
15-06-2016 02:16:46
SQL>SQL> UPDATE Prestamo SET FECHADEV=to_date('11-06-2016','DD-MM-YYYY') WHERE idLeсtor=1;
3 rows updated.
SQL> commit;
Commit complete.
SQL> select * from prestamo;
IDLECTOR IDLIBRO FECHAPRES FECHADEV FECHAREAL MORA ESTADO
---------- ---------- --------- --------- --------- ---------- ----------
1 1 09-JUN-16 11-JUN-16 5 0
1 2 09-JUN-16 11-JUN-16 5 0
1 3 09-JUN-16 11-JUN-16 5 0
SQL>