How is a trigger that adds a row and that is assigned in a column?

0

Well the fact is that I want to make a sum of a row and that this is saved in a field here of how I carry the table:

the case is that when entering:

UPDATE enero
            SET  totalgastos= enero.alimentacion+enero.transporte+enero.servicios+enero.otros

I made a sum, as we can see:

good the case is that when changing a value of any field (feeding, transport, services, others) this sure having the same value by which it would have to re-enter the:

UPDATE enero
                SET  totalgastos= enero.alimentacion+enero.transporte+enero.servicios+enero.otros

so I saw the task of creating a trigger to perform the addition automatically, when changing a value in the table:

CREATE TRIGGER 'actualizarenero' BEFORE UPDATE ON 'enero' FOR EACH ROW UPDATE enero SET totalgastos= enero.alimentacion+enero.transporte+enero.servicios+enero.otros

The problem is that trying to change a value does not leave me and I get the following error:

and I do not know what I'm doing wrong: /.

PS: Thanks for the attention: v

    
asked by pintowJD 24.09.2018 в 03:03
source

1 answer

0

The problem you have is that you can not modify by means of a trigger the table that invoked that event. In this particular case, before using a trigger it is better to use a generated field , which works as a formula that is updated as you update the associated fields. In your case, in the structure of the table modify:

'totalGastos' DOUBLE AS (ingreso + alimentacion + ... + otros)

Now, it does not come to your question, but it is advisable to normalize using a table of expenses and one of summary / totals, in that case you could use a trigger.

    
answered by 24.09.2018 / 03:34
source