What is wrong in this case since returning custom set of type from postgres function

元气小坏坏 提交于 2021-02-11 15:08:05

问题


My Table structure is

CREATE TABLE dev.clbk_logs
(
    id bigint NOT NULL,
    clbk_typ character varying(255) COLLATE pg_catalog."default",
    clbk_json json,
    cre_dte timestamp without time zone,
    ld_id bigint,
    ld_num character varying(255) COLLATE pg_catalog."default",
    mod_dte timestamp without time zone,
    CONSTRAINT clbk_logs_pkey PRIMARY KEY (id)
)
WITH (
    OIDS = FALSE
)

And my stored procedure as follows

CREATE OR REPLACE FUNCTION dev.get_ranged_loads(p_callback_types TEXT[],p_loads TEXT[], p_days_ago_1 INT, p_days_ago_2 INT) 
RETURNS table(
    clbk_json json,
    ld_id character varying,
    ld_num character varying,
    days_ago int)

AS $BODY$ 
BEGIN
    return query    
    
    SELECT a.clbk_json,a.ld_id,a.ld_num,(current_date - a.cre_dte::date) as _days_ago 
    FROM dev.clbk_logs a 
    WHERE exists (
    SELECT * from json_array_elements_text(a.clbk_json -> 'ReferenceNumbers') as x(item) 
    WHERE x.item = any(p_loads) 
    AND a.clbk_typ = any(p_callback_types)
    AND ((current_date - a.cre_dte::date)=p_days_ago_1 OR (current_date - a.cre_dte::date)<p_days_ago_2) 
) order by  a.cre_dte desc;
       
END;
$BODY$
LANGUAGE plpgsql VOLATILE;

Currently this is giving me an error

ERROR: cannot change return type of existing function DETAIL: Row type defined by OUT parameters is different. HINT: Use DROP FUNCTION dev.get_ranged_loads(text[],text[],integer,integer) first. SQL state: 42P13

What is getting wrong in above code

I am expecting the result set as

|---------|---------|------------|----------------|----------------|----------------|----------------|------------|
| id      |clbk_typ |clbk_json   |cre_dte         |          ld_id |ld_num          |mod_dte         |days_ago    |
|---------|---------|------------|----------------|----------------|----------------|----------------|------------|
| 1172    |ADD      | {}         |  0             |  12            |                |                |           | 
| 1172    |UPDATE   | {}         |  40            |  45            |                |                |           | 

Thanks in advance


回答1:


The error message is pretty clear. You can't change the return type. Run:

DROP FUNCTION dev.get_ranged_loads(TEXT[],TEXT[],INT,INT);

and then you'll be able to run CREATE OR REPLACE as usual.



来源:https://stackoverflow.com/questions/64965924/what-is-wrong-in-this-case-since-returning-custom-set-of-type-from-postgres-func

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!