在postgresql函数中捕获select查询返回值并使用它
问题描述:
我想执行这个函数。但它得到了错误说在postgresql函数中捕获select查询返回值并使用它
ERROR:
syntax error at or near ":="
LINE 7: select result:=MAX(path_history_id)as path INTO result from...
在这个功能我想:
- 执行
select with (MAX)
,它会从表中返回最大ID; - 捕获该值(它是一个整数值);
- 将该值放入最后选择查询的条件。
我不能在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';
thanks.its work.it对我来说意义重大 – Dise