-1

我正在编写以下 pgsql 程序:

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;

现在查询为提供给过程的每个参数值返回 5 行。我怎样才能返回这 5 行。此外,我如何为存储在表多边形中作为列 gid 的所有参数值执行该过程。请有人提供完整的代码。谢谢你。

4

1 回答 1

1

您可以使用RETURNS TABLE语法隐式创建OUT变量:

CREATE OR REPLACE FUNCTION KNN(
    gid_ integer
) RETURNS TABLE (
    source  integer,
    neighbor integer
) LANGUAGE SQL AS $$
    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;
$$;

要使用它,请使用它,SELECT * FROM KNN(42)您将获得最多五个两列的行。

于 2012-05-06T06:45:21.710 回答