Postgresql plpgsql函数语法错误

时间:2017-07-07 01:31:51

标签: postgresql plpgsql

我想创建一个返回一组记录的函数,但是我得到一个语法错误。查询正在运行,但是当我将其嵌入到函数中时,我收到错误。可以做些什么来返回集合?

  

错误
  错误:“select”或附近的语法错误
  第27行:选择id,user_id,project_id,record_date,parselid,pars ...
   ________ ^
  **********错误**********
      错误:“select”或附近的语法错误
      SQL状态:42601
      性格:832

功能代码

CREATE OR REPLACE FUNCTION getloggeom(
    IN p_lat double precision,
    IN p_lng double precision,
    OUT id integer, 
    OUT user_id bigint, 
    OUT project_id bigint, 
    OUT record_date timestamp without time zone, 
    OUT parselid integer, 
    OUT parselno character varying, 
    OUT adano character varying, 
    OUT ilid integer, 
    OUT ilad character varying, 
    OUT ilceid integer, 
    OUT ilcead character varying, 
    OUT mahallead character varying, 
    OUT mahalleid integer, 
    OUT pafta character varying, 
    OUT nitelik character varying, 
    OUT mevkii character varying, 
    OUT yuzolcum character varying, 
    OUT alan double precision, 
    OUT lat double precision, 
    OUT lng double precision, 
    OUT feature json)
  RETURNS SETOF record AS
$BODY$
    select id, user_id, project_id, record_date, parselid, parselno, adano, ilid, ilad, ilceid, ilcead, mahallead, mahalleid, pafta, nitelik, mevkii, yuzolcum, alan, st_x(st_centroid(st_transform(geom,4326))) as lng, st_y(st_centroid(st_transform(geom,4326))) as lat, (SELECT row_to_json(fc) as data
                     FROM ( SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) as features
                     FROM ( SELECT 'Feature' as type
                        , ST_AsGeoJSON(st_transform(ST_MakeValid(geom),4326))::json as geometry
                        , row_to_json(( parselid, ilad, ilcead, mahallead, adano, parselno, yuzolcum, st_y(st_centroid(st_transform(geom,4326))), st_x(st_centroid(st_transform(geom,4326))))) As properties
                       FROM log_geom where st_intersects((st_transform(st_setsrid(st_point(p_lng, p_lat), 4326), 500000)),geom ) limit 1)as f )  as fc) as feature  from log_geom where         
        ST_Intersects(
        st_transform(st_setsrid(st_point(p_lng, p_lat), 4326), 500000),geom
    )limit 1;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 1000
  ROWS 1000;

1 个答案:

答案 0 :(得分:0)

如果您使用plpgsql,则必须包含begin / end statements,并且必须按照doc使用RETURN QUERY ... RETURN;

否则,您可以切换到使用SQL而不是PLPGSQL,您的功能将按原样运行。

CREATE OR REPLACE FUNCTION getloggeom([...])
  RETURNS SETOF record AS
$BODY$
    [...]
$BODY$
  LANGUAGE SQL
  COST 1000
  ROWS 1000;