2016-04-28 162 views
1

我想執行這個函數。但它得到了錯誤說在postgresql函數中捕獲select查詢返回值並使用它

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. 將該值放入最後選擇查詢的條件。

我不能在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 

回答

1

語法錯誤

你的函數中的第一個查詢需要進行如下更改:

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'; 
+0

thanks.its work.it對我來說意義重大 – Dise

相關問題