As you can see in title, I want to loop through specific column which holds building IDs and then function will execute a second query that uses these looped values and show query result in PostgreSQL.
Please note that the code below:
CREATE OR REPLACE FUNCTION getBID() RETURNS SETOF building AS
$BODY$
DECLARE
r building%ROWTYPE;
BEGIN
FOR r IN EXECUTE 'SELECT point_id FROM building'
LOOP
RETURN QUERY EXECUTE 'SELECT gid, nof, year
FROM building
WHERE point_id = ' || r;
END LOOP;
RETURN;
END
$BODY$
LANGUAGE 'plpgsql' ;
SELECT * FROM getBID();
My building IDs are integer values. I wonder two aspects as follows:
- "r" variable should be declared as "integer"?
- my second query should be used in loop, too?
Thanks in advance...