1

Возвращаю строку

                 if (f.entered > tss2 and f.ended <= tse2 and tss != f.ended and f.ended > tss)
                 then 

                    tseb =   (f.ended + f.duration * interval '1 second')::timestamp(0);           
                    RETURN NEXT  ( f.ended , tseb, 
                                    r.login,
                                    r.status,
                                    r.reason,
                                    r.duration,
                                    r.t_dur
                                    );                    
                 end if;

получаю

ERROR: returned record type does not match expected record type DETAIL: Returned type timestamp without time zone does not match expected type timestamp(6) without time zone in column 2. CONTEXT: PL/pgSQL function get_all_foo2(timestamp without time zone,timestamp without time zone) line 47 at RETURN NEXT

при этом ended - timestamp, duration - integer

не могу понять как привести к валидному типу

композитный тип

CREATE TYPE public.composite_type AS (
  ended TIMESTAMP(0) WITHOUT TIME ZONE,
  entered TIMESTAMP(0) WITHOUT TIME ZONE,
  login VARCHAR(100),
  status VARCHAR(100),
  reason VARCHAR(100),
  duration INTEGER,
  t_dur TIME(0) WITHOUT TIME ZONE
);

tseb =   to_timestamp(f.ended + f.duration * interval '1 second', 'YYYY-MM-DD HH24:MI:SS')::timestamp without time zone; 

тоже не проходит

HINT: No function matches the given name and argument types. You might need to add explicit type casts. QUERY: SELECT to_timestamp(f.ended + f.duration * interval '1 second', 'YYYY-MM-DD HH24:MI:SS')::timestamp without time zone CONTEXT: PL/pgSQL function get_all_foo2(timestamp without time zone,timestamp without time zone) line 45 at assignment

а значение валидное 2016-05-21 12:17:00

2

1 ответ 1

2

Минимальный пример, который просил:

drop type fnsomefunc_type cascade;
create type fnsomefunc_type as (
    started TIMESTAMP(6) WITHOUT TIME ZONE,
    ended TIMESTAMP WITHOUT TIME ZONE
);

CREATE OR REPLACE FUNCTION fnsomefunc()
    RETURNS fnsomefunc_type AS
$$
DECLARE
    res timestamp;
BEGIN
    res = (now()::timestamp + 10 * interval '1 second');
    RETURN (res, now()::timestamp);
END;
$$
LANGUAGE 'plpgsql' IMMUTABLE;

melkij=# select fnsomefunc();
ERROR: returned record type does not match expected record type
DETAIL: Returned type timestamp without time zone does not match expected type timestamp(6) without time zone in column 1.
CONTEXT: PL/pgSQL function fnsomefunc() while casting return value to function's return type

Похоже на наблюдаемую ошибку? Вроде бы похоже. Воспользуемся явным приведением типов:

res = (now()::timestamp + 10 * interval '1 second');
RETURN (res::timestamp(6), now()::timestamp);

melkij=# select fnsomefunc();
("2016-09-15 12:18:46.598622","2016-09-15 12:18:36.598622")

Да, timestamp и timestamp(6) - рассматриваются как разные типы данных.

1
  • действительно tseb = f.ended + f.duration * interval '1 second'; RETURN NEXT ( f.ended , tseb::timestamp(0), r.login, r.status, r.reason, r.duration, r.t_dur );
    – des1roer
    15 сен 2016 в 9:39

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.