2013-11-02 40 views
0

如何将下面的这些触发器保存到一个触发器中?如何设置mysql多触发器

我试过了,但我不确定我在做什么错误?

下面是我触发的

  1. create trigger Disminuir_Existencia1 
    after insert 
    on detalle 
    for each row 
    update producto set existencia = 
             existencia-new.Cantidad 
             where id_p=new.id_p 
    
  2. create trigger Aumentar_Existencia 
    after insert 
    on detalle 
    for each row  
    
    update producto set existencia =if(new.activo = 0, producto.existencia + 
    new.cantidad,producto.existencia) 
    
    where new.id_P = producto.id_P 
    
  3. CREATE TRIGGER aumentar 
    AFTER insert 
    on detalle 
    for each row 
    
    update factura set total = 
    ( 
        select producto.precio * new.cantidad 
        from producto 
        where new.id_p=producto.id_p) 
    
    where new.Folio= factura.folio; 
    

回答

1

您的情况的正确解决方案可能取决于您已经尝试过的内容以及您尝试的结果。

例如,它可能会变成你只需要附上UPDATE报表BEGIN ... END,这将是足够的:

create trigger CombinedDetalleInsertTrigger 
after insert 
on detalle 
for each row 
begin 
    update ... where id_p=new.id_p; 

    update ... where new.id_P = producto.id_P; 

    update ... where new.Folio= factura.folio; 
end; 

this answer suggests

在许多情况下,但是,你也将需要使用DELIMITER指令:

delimiter $$ 

create trigger CombinedDetalleInsertTrigger 
after insert 
on detalle 
for each row 
begin 
    update ... where id_p=new.id_p; 

    update ... where new.id_P = producto.id_P; 

    update ... where new.Folio= factura.folio; 
end$$ delimiter ; 

this answer解释。

尽管如此,并不总是可以使用DELIMITER,因为这是客户端指令rather than a MySQL statement,并非所有的MySQL客户端都支持它。在这种情况下,您可以尝试遵循this answer的建议:使用allowMultiQueries=true连接属性或删除最后的;end之后的那个)。

0

MySQL不支持的一个触发所有例如像SQL服务器做的操作。

你这样做你必须在MySQL中。

如果您希望集中代码,您可以创建一个过程并使用NEW/OLD信息和事件标识符(I/U/D)从3个不同的触发器中调用相同的过程。