2017-07-07 10 views
1

レコードセットを返す関数を作成したいが、構文エラーが出る。クエリは機能していますが、関数に埋め込んだときにエラーが発生します。セットを返すために何ができるのですか?Postgresql plpgsql関数の構文エラー

Error
ERROR: syntax error at or near "select"
LINE 27: select id, user_id, project_id, record_date, parselid, pars...
________^
********** Error **********
ERROR: syntax error at or near "select"
SQL state: 42601
Character: 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; 

答えて

0

あなたがplpgsqlがを使用している場合、あなたはbegin/endstatementsを含める必要がありますし、あなたがdocあたりとしてRETURN QUERY ... RETURN;使用する必要があります。

それ以外の場合は、PLPGSQLの代わりにSQLを使用すると、機能はそのまま動作します。

CREATE OR REPLACE FUNCTION getloggeom([...]) 
    RETURNS SETOF record AS 
$BODY$ 
    [...] 
$BODY$ 
    LANGUAGE SQL 
    COST 1000 
    ROWS 1000; 
関連する問題