PostgreSQL - SQL Error [42601]: ERROR: query has no destination for result data

PostgreSQL - SQL Error [42601]: ERROR: query has no destination for result data

我正在创建一个函数来更新我 table 中的记录并根据结果输出一个 json 文档,但是我收到一个没有目的地的结果错误。

我使用的代码是:

create or replace function update_frequency(_id int,_shipping_method_id int,_day_of_week frequency,_time_range timerange, _maximum_orders int, p_time_range timerange, out out_json json)
  returns json as
$$

begin
 select time_range into p_time_range from "_frequence" where id = _id;
    if not found then 
         raise EXCEPTION '{"error":"frequence id not found"}';
    else 
        if p_time_range = _time_range then
            raise notice '{"error":"they are equal"}';
            WITH upd1 AS (
                update "_frequence" set 
                    id = _id,
                    shipping_method_id = _shipping_method_id,
                    day_of_week = _day_of_week,
                    maximum_orders = _maximum_orders
                where id = _id
                RETURNING id,shipping_method_id, day_of_week, time_range,maximum_orders
            )
            select row_to_json(t) from (
                Select id, shipping_method_id, day_of_week, time_range ,maximum_orders
                FROM upd1 where upd1.id = id
            )t;
            out_json = row_to_json(t);
        elseif (p_time_range && _time_range) then
            raise EXCEPTION '{"error":"they are overlapping"}';
        else
            WITH upd1 AS (
                update "_frequence" set 
                    id = _id,
                    shipping_method_id = _shipping_method_id,
                    day_of_week = _day_of_week,
                    time_range = _time_range,
                    maximum_orders = _maximum_orders
                where id = _id
                RETURNING id,shipping_method_id, day_of_week, time_range,maximum_orders
            )
                select row_to_json(t) from (
                    Select id, shipping_method_id, day_of_week, time_range ,maximum_orders
                    FROM upd1 where upd1.id = id
                )t;
            out_json = row_to_json(t);
        end if;
    end if; 
end;
$$ LANGUAGE plpgsql;

函数调用如下:

select * from update_frequency(5, 1, 'Tuesday', timerange(time '20:00', time '20:59', '[]'), 4,null);

我收到这个:

SQL错误[42601]:错误:查询没有结果数据的目的地 提示:如果您想丢弃 SELECT 的结果,请改用 PERFORM。 其中:PL/pgSQL function update_frequency(integer,integer,frequency,timerange,integer,timerange) line 10 at SQL statement

我不确定出了什么问题,需要别人的帮助。

您必须在查询中包含分配:

WITH (...)
SELECT row_to_json(t) INTO out_json
FROM ...;