i am writing a folloing pgsql procedure :
CREATE OR REPLACE FUNCTION KNN(gid_ integer)
RETURNS Text AS $body$
DECLARE
row_ RECORD;
BEGIN
SELECT g1.gid As SOURCE, g2.gid As Neighbors FROM polygons as g1, polygons as g2 WHERE g1.gid = $1 and g1.gid <> g2.gid ORDER BY g1.gid,
ST_Distance(g1.the_geom,g2.the_geom) limit 5;
END
$body$
LANGUAGE plpgsql;
Now that the query return 5 rows for each value of arrgument supplied to procedure. How can i return those 5 rows. Also, how can i execute the procedure for all values of argument stored in a table polygons as column gid. Please somebody give the full code please. thankful to you.
You can use the
RETURNS TABLEsyntax to implicitly createOUTvariables:To use it, use
SELECT * FROM KNN(42)and you will get back up to five two-column rows.