我必须将lat和long转换为geom
才能使用PostGIS。我的问题是,我有来自不同位置的各种表格,我想将表格作为参数传递给函数。我试试这个:将表格作为参数传递
CREATE or REPLACE FUNCTION convert_from_lon_lat(float,float,character varying)
RETURNS integer AS $$
select id from $3 as vertices
order by vertices.geom <-> ST_SetSrid(ST_MakePoint($1,$2),4326) LIMIT 1;
$$ LANGUAGE SQL;
但我得到一个语法错误。
EDIT1:
所以我改变了以前的代码这样:
CREATE or REPLACE FUNCTION convert_from_lon_lat(long float, lat float, _table character varying) RETURNS integer AS $$
BEGIN
EXECUTE('select id from _table as vertices order by vertices.geom <-> ST_SetSrid(ST_MakePoint(long,lat),4326) LIMIT 1;');
END;
$$ LANGUAGE plpgsql;
它创建没有任何问题,但是当我把它叫做'convert_from_lon_lat(long1,long2,MY_TABLE)
我得到和错误:
ERROR: relation "_table" does not exist
它没有通过表名作为参数
编辑2:
CREATE or REPLACE FUNCTION convert_from_lon_lat(long float, lat float, tbl character varying) RETURNS integer AS $func$
BEGIN
EXECUTE format('select id from %s order by %s.the_geom <-> ST_SetSrid(ST_MakePoint('|| long || ','|| lat ||'),4326) LIMIT 1;', tbl, tbl);
END;
$func$ LANGUAGE plpgsql;
现在,当我调用该函数,我得到一个'错误:控制达到的功能结束无RETURN``
我试图RETURN QUERY EXECUTE format('...
但我得到一个ERROR: cannot use RETURN QUERY in a non-SETOF function
您可能需要使用PL/pgSQL的和一个动态SQL构造。 – dezso
我很喜欢postgres,我该怎么做? – tvieira