Below is the psql code in which I am having syntax error. I am trying to create a trigger function on my test_route database

China☆狼群 提交于 2020-07-03 13:26:42

问题


CREATE OR REPLACE FUNCTION nearest_segment_insert()
RETURNS trigger AS
DECLARE id_temp integer;
$BODY$
BEGIN
    IF OLD.id = NULL THEN
         select test_route.id into id_temp,ST_Distance(test_route.the_geom,NEW.the_geom)
         FROM test_route,
         ORDER BY 2 ASC LIMIT 1;

         UPDATE SET p.edges_id = r.id, p.the_geom = r.the_geom, p.dist_val = r.distance, r.distance = -1 
         FROM test_route r, road_block p,
         WHERE id_temp = r.id ;
    END IF;

    RETURN NEW;
END;
$BODY$

I am getting error "syntax error at or near "BEGIN".


回答1:


The error you are getting is because this:

CREATE OR REPLACE FUNCTION nearest_segment_insert()
RETURNS trigger AS
DECLARE id_temp integer;
$BODY$
...

should be:

CREATE OR REPLACE FUNCTION nearest_segment_insert()
RETURNS trigger
LANGUAGE plpgsql
AS
$BODY$
DECLARE id_temp integer;
...

For more information see:

https://www.postgresql.org/docs/current/plpgsql-structure.html



来源:https://stackoverflow.com/questions/62378772/below-is-the-psql-code-in-which-i-am-having-syntax-error-i-am-trying-to-create

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!