skip to Main Content

The code below is not compiling by HeidiSQL in a MYSQL base

BEGIN

    if NOT EXISTS (SELECT * FROM pedido_itens WHERE ID_PEDIDO = OLD.ID_pedido)
        DELETE from pedido WHERE pedido.ID = pedido_itens.old.ID_PEDIDO;
        END if;
END

I’m trying to create a trigger to remove from the "pedido" table when there are no more items in the "pedido_itens" table. But I can’t compile, I don’t understand where I’m going wrong. Thanks in advance for all help.
I’m new in the area.

2

Answers


  1. Chosen as BEST ANSWER

    I got it, had to change some details. But mostly the "then" was missing

        BEGIN
    
        IF (SELECT COUNT(*) pedido_itens WHERE ID_PEDIDO = OLD.ID_pedido) >0 THEN
        
            DELETE from pedido WHERE pedido.ID = OLD.ID_PEDIDO;
            END IF; 
        END
    

  2. with if statement you should use ‘then’,it is for what action you want to perform against your if.

    BEGIN
            IF (SELECT COUNT(*) pedido_itens WHERE ID_PEDIDO = pedido_itens.OLD.ID_pedido) > 0 
            THEN
                DELETE from pedido WHERE pedido.ID = pedido_itens.OLD.ID_PEDIDO;
                END IF; 
            END
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search