2

I want to make the Total column receive the total sum between the other columns, but i keeping getting the error 1193. I'm new to mySql so i dont know where i should start looking to solve the error.

DELIMITER $$
CREATE TRIGGER Total
BEFORE INSERT ON `despesas` FOR EACH ROW
BEGIN
    SET Total = Abertura_Processo+Telefone+Correspondencia+Cartorio+Conservatorio+Servico_Financas+Taxas_Emolumentos+Honorarios;
END;
$$
DELIMITER ;

I've read a bit and for what i've understood I need to create a variable, but i dont know how. The total column has its values manually inserted i want it to be automatically

2
  • 1
    What do you want to do with the total value? Commented May 31, 2017 at 20:31
  • I want to have the total value inserted into the database on the column total, so i can search for it. For example I searched for the payement with Id=1 with where Abetura_Processo=10 and Telefone=10 and so on, then i want the total column to be equal to 80. Commented May 31, 2017 at 20:34

2 Answers 2

3

You can reference the newly inserted record's field via NEW.column_name in the before insert trigger. By setting a value to such a column, you can change the value being inserted. So, change the value setting line as follows:

SET NEW.Total = NEW.Abertura_Processo+NEW.Telefone+NEW.Correspondencia+NEW.Cartorio+NEW.Conservatorio+NEW.Servico_Financas+NEW.Taxas_Emolumentos+NEW.Honorarios;

Note, you may use generated columns as an alternative to this trigger.

Sign up to request clarification or add additional context in comments.

Comments

0

Got it, every thing worked fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.