Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在postgresql函数中捕获select查询返回值并使用它_Postgresql_Plpgsql - Fatal编程技术网

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

在postgresql函数中捕获select查询返回值并使用它,postgresql,plpgsql,Postgresql,Plpgsql,我想执行这个函数。但它得到了错误的回答 错误: 在“:”处或附近出现语法错误 第7行:选择result:=MAX(路径\历史\ id)作为从…进入结果的路径 在这个函数中,我想: 执行select with(MAX)将从表中返回最大id 捕捉该值(它是一个整数值) 将该值放入最后一个select查询where条件 我在postgresql中找不到这样做的方法 CREATE OR REPLACE FUNCTION memcache(IN starting_point_p1 character va

我想执行这个函数。但它得到了错误的回答

错误:

在“:”处或附近出现语法错误

第7行:选择result:=MAX(路径\历史\ id)作为从…进入结果的路径

在这个函数中,我想:

  • 执行
    select with(MAX)
    将从表中返回最大id
  • 捕捉该值(它是一个整数值)
  • 将该值放入最后一个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';