1

I want to execute this function. But it got error said

ERROR:

syntax error at or near ":="

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

In this function I want to:

  1. execute select with (MAX) and it will return maximum id from a table;
  2. catch that value (it is an integer value);
  3. put that value into last select query where condition.

I cant find a way in postgresql to do this.

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
0

1 Answer 1

1

Syntax Error

The first query inside your function needs to be changed as follows:

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

A single Query

You don't actually need a stored procedure for this. A single query can achieve the same result.

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';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.