i have created a table trans in which i want to store data after a insert into table resultate. When I run my trigger, I get back two much data in trans, it seems that for every each row the trigger store the complete statement into the db. How I have to change my trigger that just at the end of the insert statement into resultate, the trigger store the statment once into table trans?
Here is my trigger...
CREATE TRIGGER trans_trigger
AFTER INSERT
ON resultate FOR EACH ROW
insert into trans(ID_trans, Spieltag, Heimteam, ID)
SELECT
CASE
WHEN @Heimteam = Heimteam THEN @ID_before
ELSE NULL
END AS ID_Final,
Spieltag,
@Heimteam:=Heimteam as Heimteam,
@ID_before:=IDer as ID
FROM
resultate
ORDER BY Heimteam, Spieltag;
transfor every row inresultate. That's because it's running aINSERT ... SELECT ... FROM resultatewith noWHEREclause. To reference just the row that was just inserted, remove the queryFROM resultateand useNEW.heimteam,NEW.spieltag, etc. to reference values of the row that was just inserted. - spencer7593resultate? Yes there is... avoid doing anINSERT ,,, SELECT ...that gets every row inresultate. Maybe limit the number of rows returned by the SELECT. Maybe wrap that query in the trigger inside a another SELECT (use the existing query as an inline view), and maybe add a WHERE clause on the outer query, with NEW.col references that limits the rows returned. - spencer7593