Oracle - Ayuda Trigger

 
Vista:

Ayuda Trigger

Publicado por Uriel (1 intervención) el 08/11/2014 05:06:21
Disculpen alguien podría ayudarme con este trigger
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
create or replace trigger ai_libro
after insert on libro
for each row
 
DECLARE
  total NUMBER(4);
  promedio NUMBER(10);
 
begin
  SELECT total_libros+1 into total
  FROM estadistica_genero
  where genero=:new.genero;
 
  SELECT avg(precio) into promedio
  FROM LIBRO
  where genero=:new.genero;
 
update estadistica_genero
  set TOTAL_LIBROS=total,PRECIO_PROMEDIO=promedio
  where genero=:new.genero;
 
end;
Lo que tiene que hacer es que cada vez que se inserte un nuevo libro en la tabla libro se actualice la tabla estadistica_genero.

Pero cuando inserto un libro me manda este error.
1
2
3
4
5
6
7
8
9
10
11
INSERT INTO LIBRO
VALUES('9781412160711','novela','El pendulo',50,100,'Edgar Alan',' ')
Informe de error -
Error SQL: ORA-04091: table LIBRERIA.LIBRO is mutating, trigger/function may not see it
ORA-06512: at "LIBRERIA.AI_LIBRO", line 9
ORA-04088: error during execution of trigger 'LIBRERIA.AI_LIBRO'
04091. 00000 -  "table %s.%s is mutating, trigger/function may not see it"
*Cause:    A trigger (or a user defined plsql function that is referenced in
           this statement) attempted to look at (or modify) a table that was
           in the middle of being modified by the statement which fired it.
*Action:   Rewrite the trigger (or function) so it does not read that table.
Si alguien me pudiera ayudar le estaría muy agradecido.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Ayuda Trigger

Publicado por Manuel (1 intervención) el 09/11/2014 14:32:42
Hola, realmente no se puede hacer un insert y al mismo tiempo en la misma tabla un update.... dentro del trigger INSERT, podrías crear, para el UPDATE otro trigger AFTERINSERT.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 499
Oro
Ha mantenido su posición en Oracle (en relación al último mes)
Gráfica de Oracle

Ayuda Trigger

Publicado por Rafael (328 intervenciones) el 11/11/2014 11:25:32
Hola:

Tu problema pasa por que haces una consulta a la misma tabla que estas modificando:
1
2
SELECT avg(precio) into promedio
FROM LIBRO where genero=:new.genero;

El sistema se protege ya que a pesar que has colocado el trigger como AFTER INSERT este realmente no se ha realizado se encuentra en medio de una transaccion implicita.

Luego entonces hay algunos detalles que debes considerar:
- El registro que estas insertando ha sido bloqueado hasta terminar el total de la operación, esto no ocurre hasta que se realice el commit de la transacción.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar