错误4082查询结构与函数结果类型不匹配

时间:2017-08-14 05:52:56

标签: sql postgresql

我是postgre中的新手,我试图使用以下代码编写存储过程:

CREATE OR REPLACE FUNCTION public.get_month_status()
returns table (request_detail character varying, request_detail2 character 
varying, request_detail3 character varying)
language plpgsql stable
as $function$
        BEGIN       
        return query

            select
            CASE
                WHEN (extract(DAY FROM now()) >= 25) THEN (extract(MONTH FROM now()) || '-' || extract(Year FROM now()))
                ELSE (extract(MONTH FROM now()) - 1 || '-' || extract(Year FROM now()))
            end,
            CASE
                WHEN (extract(month FROM now()) = 2) THEN (extract(month FROM now()) -1 || '-' || extract(Year FROM now()))
                ELSE (extract(month FROM now()) || '-' || extract(Year FROM now()))
            end,
            CASE
                WHEN (extract(month FROM now()) = 1) THEN (extract(month FROM now() - interval '2 months' ) || '-' || extract(Year FROM now()) -1)
                ELSE (extract(month FROM now()) || '-' || extract(Year FROM now()))
            end;
            end;
    $function$;

但每当我尝试使用select * from get_month_status()执行命令时,我都会收到错误

SQL Error [42804]: ERROR: structure of query does not match function result type
Detail: Returned type text does not match expected type character varying in column 1.
Where: PL/pgSQL function get_month_status() line 3 at RETURN QUERY
org.postgresql.util.PSQLException: ERROR: structure of query does not match 
function result type
Detail: Returned type text does not match expected type character varying in column 1.
Where: PL/pgSQL function get_month_status() line 3 at RETURN QUERY

我如何让它发挥作用?有人帮忙!

1 个答案:

答案 0 :(得分:1)

错误消息说错了:

  

返回的类型文本与预期的类型字符变化不匹配

只需将返回的表格列类型更改为text并重新创建功能

即可
... returns table (request_detail text, request_detail2 text, request_detail3 text) ...