查询的结构与函数结果类型不匹配

Nul*_*lik 2 postgresql plpgsql

将类型(通过强制转换)从 bigint 更改为 text 后,我​​的 PLPGSQL 函数停止工作。这是我得到的错误:

dev=> select * from app.get_companies(4,808739954140037) ;
NOTICE:  Data rows were NOT found (structure of query does not match function result type)
 company_id_str | company_name 
----------------+--------------
(0 rows)

dev=> 
Run Code Online (Sandbox Code Playgroud)

这是我的功能:

CREATE OR REPLACE FUNCTION app.get_companies(ident_id bigint,sess bigint)
RETURNS TABLE(company_id_str text,company_name text) as $$
DECLARE
    server_session bigint;
BEGIN
    select app.session.session from app.session where app.session.identity_id=ident_id and app.session.session=sess into server_session;
    IF FOUND
    THEN
        BEGIN
            RETURN QUERY SELECT quote_ident(app.company.company_id::text)::text as company_id_str,app.company.name as company_name FROM app.company,app.identcomp WHERE app.company.company_id=app.identcomp.company_id and app.identcomp.identity_id=ident_id;
        EXCEPTION
            WHEN OTHERS THEN
                RAISE NOTICE 'Data rows were NOT found (%)',SQLERRM;
                RETURN;

        END;
    ELSE
        RAISE NOTICE 'Session row was NOT found';
        RETURN;
    END IF;
END;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)

如果我正在应用演员表并将输出定义为 TABLE ,为什么会发生此错误?

app.company 表定义为:

create table app.company (
    company_id          bigserial,
    date_inserted       timestamp,
    name                varchar(64)
);
Run Code Online (Sandbox Code Playgroud)

kli*_*lin 6

app.company.nameis varchar(64)whilecompany_name返回的表是text。投射app.company.nametext.

如果你没有在函数体中捕获异常(为什么?)你会得到更详细的错误信息:

ERROR:  structure of query does not match function result type
DETAIL:  Returned type character varying(64) does not match expected type text in column 2.
Run Code Online (Sandbox Code Playgroud)