Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which SQL statement can I use to re-execute the trigger and update all the rows in a table?

I have a table with a lot of rows and I have changed the function associated with the trigger in insert or update events. This function makes calculations to update columns with conditional logic according to the values in two or more cells, so an single update statement would not be useful.

So, which SQL statement can I use to re-execute the trigger and update all the rows?

Thank you.

like image 452
Alex. S. Avatar asked Dec 05 '22 06:12

Alex. S.


2 Answers

You have to update a column with exactly the same value. Of course it depends on the conditions for trigger. For instance:

UPDATE table
  SET columnX = columnX;

Any way, as a best-practices for this cases I usually have and associated function in which I can run something like (not sure if works for progresql):

BEGIN
  for c in (select column_id from table)
  loop
    function_on_trigger(c.column_id);
  end loop;
END;
/
like image 167
FerranB Avatar answered May 18 '23 21:05

FerranB


In response to FerranB's answer, the correct way to run the function in Postgres would be the following:

SELECT function_on_trigger(column_id) FROM table;
like image 21
Kenaniah Avatar answered May 18 '23 20:05

Kenaniah