Error in Trigger Sqlite

Asked

Viewed 151 times

3

I’m trying to create this trigger in the SQLite, but I don’t know if it’s possible.

I got a big one Remedios and a Manutenção. I want that when I have an insert in the table Manutenção, it does an update depending on the condition on Case, but is making a mistake in Update.

CREATE TRIGGER TGmovimentacao 
    AFTER INSERT 
    ON manutencao
    for each row 
        BEGIN
            Select Case
                When ( NEW.tpMov = 'S' )
                 then UPDATE Remedio set Remedio.RemedioDose = Remedio.RemedioDose - NEW.qtdDose where Remedio.idRemedio = NEW.idManutencao
            END;
        END;
  • What’s the mistake?...

  • then, in Sqlite Studio it will underline red Update forward.

  • in Android Studio, tbm says that the error is next to Update

  • 1

    So it’s syntax error. You can’t execute an instruction after the Then, just return values. Look here for example

1 answer

2


Your Rigger displays some syntax errors. Here is an alternative

CREATE TRIGGER TGmovimentacao AFTER INSERT ON manutencao
  WHEN NEW.tpMov = 'S'
  BEGIN
     UPDATE Remedio 
        SET Remedio.RemedioDose = Remedio.RemedioDose - NEW.qtdDose 
      WHERE Remedio.idRemedio = NEW.idManutencao
  END;

The instruction FOR EACH ROW is optional since Sqlite only supports FOR EACH ROW triggers and not FOR EACH STATEMENT triggers.

Edit: This version should update the value of Remediodose according to tpMov.

CREATE TRIGGER TGmovimentacao AFTER INSERT ON manutencao
  BEGIN
     UPDATE Remedio 
        SET Remedio.RemedioDose = Remedio.RemedioDose + 
                CASE 
                   WHEN NEW.tpMov = 'S' THEN NEW.qtdDose * (-1) 
                   WHEN NEW.tpMov = 'E' THEN NEW.qtdDose 
                   ELSE 
                END
      WHERE Remedio.idRemedio = NEW.idManutencao
  END;
  • ok. I did it this way, but I must create another identity with NEW.tpMov = 'E ??? and add the dose ? ?

  • It should not be necessary to create another Trigger. new.tpMov = 'E' means input?

  • Yes, E = Input and S = Output

  • The second version of Trigger updates the dose value according to the type of movement.

  • vlw. I will test

Browser other questions tagged

You are not signed in. Login or sign up in order to post.