What’s wrong with Rigger?

Asked

Viewed 135 times

0

I have this table:

create table alecrim(
id_alecrim int not null auto_increment, 
sem_epi int not null,
p1 smallint,
p2 smallint,
p3 smallint,
p4 smallint,
p5 smallint, 
p6 smallint,  
p7 smallint,
p8 smallint,
p9 smallint,
totOvos int,
pend tinyint,
ext tinyint,
ano varchar(4),
primary key(id_alecrim)
) default charset = utf8;

And this Rigger:

CREATE TRIGGER somaOvosIFS BEFORE UPDATE
ON alecrim
FOR EACH ROW
DELIMITER \\
    begin
    SET NEW.totOvos = NEW.p1 + NEW.p2 + NEW.p3 + NEW.p4 + NEW.p5 + NEW.p6 + NEW.p7 + NEW.p8 + NEW.p9;
    IF(old.pend > 0) THEN
    NEW.pend = old.pend - 1;
    END IF;
END \\

The goal of Trigger is to update the fields in question (until then it works) and if the Pend field is greater than 0 it updates its value with the operation in question.

Someone helps?

1 answer

2

Just lack of experience myself!

Follow the corrected TRIGGER:

delimiter //
CREATE TRIGGER somaOvosAlecrim BEFORE UPDATE ON alecrim
FOR EACH ROW
BEGIN
 IF OLD.pend > 0 THEN
 SET NEW.totOvos = NEW.p1 + NEW.p2 + NEW.p3 + NEW.p4 + NEW.p5 + NEW.p6 + NEW.p7 + NEW.p8 + NEW.p9,
 NEW.PEND = OLD.pend - 1;
 ELSEIF OLD.pend = 0 THEN
 SET NEW.totOvos = NEW.p1 + NEW.p2 + NEW.p3 + NEW.p4 + NEW.p5 + NEW.p6 + NEW.p7 + NEW.p8 + NEW.p9;
 END IF;
END;//
delimiter ;
  • Hello Amiraldo. You can accept your own answer to mark that the problem has been solved.

  • @Anthonyaccioly So you don’t need to edit the question title?

  • Exactly, there’s no need to change the headline by accepting your own answer (or that of any other member).

Browser other questions tagged

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