0

Hello guys I want to create a dynamic table query to be updated on runtime. I have two postgis tables one that contains points--tablename(records) and one that contains polygons--tablename(OpDMA). This select query:

Create table Op_DMAConn as
SELECT pol.id as polygon_id, poi.id as point_id 
FROM "OpDMA" pol
LEFT JOIN records poi ON (ST_Intersects(poi.geom, pol.geom))

returns the polygon_id polygons and point_id of the points. I would like this query to be executed on runtime!

1 Answer 1

1

I think what you are looking for is the 'EXECUTE' statement. https://www.postgresql.org/docs/current/sql-execute.html

For example you can do that :

CREATE OR REPLACE FUNCTION public.fn_pointer(points_table_name varchar, polygon_table_name varchar)
 returns  table(polygon_id int, point_id int)
LANGUAGE plpgsql AS
$$
declare
    final_query varchar;
begin
    query := 'SELECT ST_Contains(polygon.geom, point.geom)
    FROM public."'||points_table_name||'" point, public."'||polygon_table_name||'" polygon;'

    -- do what you have to do to make your query return you correct table

    return query execute final_query;
end
$$
;

select fn_pointer('table_name_1','table_name_2');

Hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

Can you tell me what it does when new points are added, does it update the table or creates a new table ?
What this code provide : a not working version of the crucial part of what you need that is the dynamix query mechanism. In this code nothing is created nor updated in database. It is just a basic select that will be tried to be returned (and will fail due to the number of awaited column required by the return type of the function.
I have created a table with the results as I want them which returns the polygon id and point id this previous select query created just the id of the points and false or true if it is inside of a polygon. I will update my select query in the question, can you help me create this execute command?
Could you please follow those guidelines : it will be helpfull to answer you correctly and in the time that I have (not so much ;) ) meta.stackoverflow.com/questions/271055/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.