In your current code, the loop doesn't make sense indeed because the UPDATE
alone already processes every row of the parcels
table.
What you probably want is (without a loop):
UPDATE parcels R SET nearest_intersection =
(select intersection.osm_id
from intersections
order by intersections.geom <-> R.geom
limit 1);
which in procedural thinking would be the equivalent to:
for every row
R
ofparcels
, find the row inintersections
whosegeom
is the nearest toR.geom
and copies itsosm_id
intoR.nearest_intersection
On the other hand, if it had to be done with a loop, it would look like this:
FOR var_gid IN SELECT gid FROM parcels
LOOP
UPDATE parcels SET nearest_intersection =
(select intersection.osm_id
from intersections
order by
intersections.geom <-> parcels.geom)
limit 1)
WHERE parcels.gid=var_gid;
end loop;