我有查询功能。如果条件为true,则用新值更新一行。
我进行查询:
SELECT * FROM transfer_flight(41313, '2017-08-15 20:00:00+05');
我得到一个错误:
错误:表“航班”缺少FROM子句条目 第1行:SELECT(flights.scheduled_departure <$ 2) ^ 查询:SELECT(flights.scheduled_departure <$ 2) 背景:IF处的PL / pgSQL函数transfer_flight(integer,timestamp with time> zone)在第7行位于
CREATE OR REPLACE FUNCTION flightFunc(
flight_identificator INTEGER,
new_timestamp timestamp with time zone)
RETURNS TABLE(
flight_id INTEGER,
flight_no CHARACTER(6),
departure_airport character(3),
scheduled_departure timestamp with time zone,
arrival_airport character(3),
scheduled_arrival timestamp with time zone
)
LANGUAGE plpgsql
AS $$
DECLARE
flight_d timestamp with time zone = scheduled_arrival -
scheduled_departure;
BEGIN
IF (flights.scheduled_departure < $2)
THEN
UPDATE flights
scheduled_departure = $2,
scheduled_arrival = $2 + flight_d
FROM flights
WHERE flights.flight_id = $1
RETURNING
flights.flight_id,
flights.flight_no,
flights.departure_airport,
flights.scheduled_departure,
flights.arrival_airport,
flights.scheduled_arrival;
END IF;
END;
$$;
结果,我只需要使用输入flight_identificator更新一行
答案 0 :(得分:0)
删除FROM
子句!就是CROSS JOIN
。并且您需要SET
。所以:
UPDATE flights
SET scheduled_departure = $2,
scheduled_arrival = $2 + flight_d
WHERE flights.flight_id = $1
RETURNING
flights.flight_id,
flights.flight_no,
flights.departure_airport,
flights.scheduled_departure,
flights.arrival_airport,
flights.scheduled_arrival;
答案 1 :(得分:0)
在我看来,您应该摆脱_GNU_SOURCE
变量和flight_d
块,然后将逻辑移至查询本身,例如:
IF