在 postgresql 函数中捕获 select 查询 return 值并使用它

catching select query return value in postgresql function and use it

我想执行这个功能。但出现错误

ERROR:

syntax error at or near ":="

LINE 7: select result:=MAX(path_history_id)as path INTO result from...

在这个函数中我想:

  1. 执行 select with (MAX) 它将 return 来自 table;
  2. 的最大 id
  3. 捕获那个值(它是一个整数值);
  4. 将该值放入最后一个 select 查询 where 条件。

我无法在 postgresql 中找到执行此操作的方法。

CREATE OR REPLACE FUNCTION memcache(IN starting_point_p1 character varying, IN ending_point_p1 character varying)

RETURNS TABLE(path integer, movement_id_out integer, object_id_fk_out integer, path_history_id_fk_out integer, walking_distance_out real, angel_out real, direction_out character varying, time_stamp_out timestamp without time zone, x_coordinate_out real, y_coordinate_out real, z_coordinate_out real) AS
$BODY$
    DECLARE result int;
    BEGIN
    
    select result:=MAX(path_history_id)as path INTO result from path_history_info where starting_point=starting_point_p1 and ending_point =ending_point_p1 and achieve='1';
    return query
    select * from movement_info where path_history_id_fk=result;    
    END;
    $BODY$
  LANGUAGE plpgsql

语法错误

函数中的第一个查询需要更改如下:

select MAX(path_history_id)as path INTO result 
  from path_history_info 
    where starting_point=starting_point_p1 
     and ending_point =ending_point_p1 and achieve='1';

单个查询

您实际上不需要存储过程。单个查询可以达到相同的结果。

select * from movement_info where path_history_id_fk = 
 (SELECT MAX(path_history_id) FROM path_history_info 
    where starting_point=starting_point_p1 
     and ending_point =ending_point_p1 and achieve='1';