MySQL Forums
Forum List  »  Spanish

Duda con Triggers y Delete
Posted by: Jorge A.A
Date: February 02, 2008 01:59AM

Tengo dos tablas: Ventas y Productos.


+--------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+----------+------+-----+---------+-------+
| Codigo | int(11) | NO | PRI | | |
| Descripccion | char(30) | NO | | | |
| Precio | float | NO | | | |
| Existencia | int(11) | NO | | 0 | |
+--------------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)

+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| Folio | char(30) | NO | PRI | | |
| Codigo | int(11) | NO | PRI | | |
| Cantidad | int(11) | NO | | | |
| Precio | float | NO | | | |
+----------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)

Y el siguiente trigger para activarse al insertar una nueva venta:

create trigger disminuir_existencia AFTER INSERT ON DETALLE_DE_VENTA

FOR EACH ROW

BEGIN

DECLARE Existencia_Producto Integer;

select PRODUCTO.Existencia into Existencia_Producto from PRODUCTO where PRODUCTO.Codigo = NEW.Codigo;

IF Existencia_Producto >= NEW.Cantidad THEN


update PRODUCTO set Existencia=Existencia-NEW.Cantidad where PRODUCTO.Codigo = NEW.Codigo;

ELSE

delete from DETALLE_DE_VENTA where Folio = NEW.Folio AND Codigo = NEW.Codigo;

END IF;




END|

Como se darán cuenta lo uso para cuando una nueva venta se realza y se ingresa en la base de datos, al mismo tiempo disminuya la existencia del producto respecto a la cantidad del mismo que se haya vendido. El error proviene al invocar la instrucción DELETE en el ELSE:

mysql> insert into DETALLE_DE_VENTA values ('200716100002',3,5,600);
ERROR 1442 (HY000): Can't update table 'DETALLE_DE_VENTA' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

Alguien sabe solucionarlo? o alguna recomendación para realizar lo que trato de hacer? (en sqlserver se podía usar transacciones con triggers y con un ROLLBACK bastaba para detener el insert en cualquier momento si fuese necesario).

Un saludo.

Options: ReplyQuote


Subject
Views
Written By
Posted
Duda con Triggers y Delete
4662
February 02, 2008 01:59AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.