capture variable in a mysql trigger

0

Good evening really thank you for your time, I tell you  I have an expense table with several fields like idgasto, vendor, client, total and one more detail_gasto that has fields idgasto, idproduct until everything is fine, but the application is web and for some information controls, I am using trigger then, I decided to create a temporary table where it has the same fields as detail_expend except idgasto for the trigger to clone the data from there to detail_gas after spending insert. The problem is how I tell the trigger to clone the data and put the corresponding id in the field idgasto .. .I hope to have been clear and again thank you

    
asked by Gilberto Asuaje 05.07.2017 в 08:10
source

1 answer

1

I think you could do it would be the following:

DELIMITER //
CREATE TRIGGER CopiarGastos AFTER INSERT ON gastos
FOR EACH ROW BEGIN  
    INSERT INTO tabla_gastos_copia select * from tabla_gastos where idgasto = NEW.idgasto;
END;//
DELIMITER ;

Basically what it does is that after inserting in the table tabla_gastos , for each row inserted in the table tabla_gastos (that will be 1) it inserts you in the table < strong> table_expenses_copy the same record.

EDIT: it is not obligatory that the copy table has NO autoincremental id, but from my point of view for this case I would not put it in case at some point you do reordering id's and others when deleting fields, it could give conflicts.

I hope I have been of help, greetings!

    
answered by 05.07.2017 / 08:52
source