gpt4 book ai didi

postgresql - 在 postgresql 函数中捕获选择查询返回值并使用它

转载 作者:行者123 更新时间:2023-11-29 13:24:05 24 4
gpt4 key购买 nike

我想执行这个功能。但是它说有错误

ERROR:

syntax error at or near ":="

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

在这个函数中我想:

  1. 执行select with (MAX),它将返回表中的最大id;
  2. 捕获那个值(它是一个整数值);
  3. 将该值放入最后一个选择查询 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';

关于postgresql - 在 postgresql 函数中捕获选择查询返回值并使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36909447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com