Skip to content
Advertisement

How to make a PG function and trigger from SQL syntax?

I’m currently using this SQL syntax to update all records of one table after checking values in another table:

Sorry I’m a newbie so I’m trying to setup a trigger after each inserting or updating of table 1, that updates field1 values calling a PG function that is able to do as above. Could you please help me? Thanks in advance

Advertisement

Answer

Why not simply create a view to display this derived information, rather than storing data that you then need to manage?

The upside is that you have an always up-to-date perspective at your data, without any maintenance cost.

This should be as simple as:

Side note: you should try and fix your schema – the joining logic indicates that it is not properly normalized. You should store each of the four diam* column of table2 in rows rather than columns, with a range specification in other columns, like:

Then the query could be simplified as the simpler and more efficient:

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