Skip to content
Advertisement

Trigger for UPDATE runs many time on batch Updates

All of my tables have a Trigger for CRUD operations. here is a sample:

If I update one row, everything works fine and trigger inserts one row in history.

For example

But if more than one row updated, updatedrow^2 rows will be inserted.

For example 9 for 3 rows 100 for 10 rows…

What is wrong with my trigger and how could I solve it?

Advertisement

Answer

You are cross joining inserted and deleted. Normally, they would be joined using the table’s primary key, which is presumably CityId:

In this case, deleted is not being used, so it does not even need to be included in the query.

You could implement the entire trigger as a single query in the table using LEFT JOIN:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement