0

My Table looks like

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
)
TABLESPACE pg_default;

My function is

CREATE OR REPLACE FUNCTION dev.my_method(p_callback_types TEXT[], p_days_ago INT) 
RETURNS SETOF dev.clbk_logs 
    LANGUAGE 'plpgsql'
AS $BODY$      
BEGIN
    return query
    SELECT * FROM dev.clbk_logs  
    WHERE  (clbk_Typ::TEXT) IN (($1))  AND (current_date - cre_dte::date)< p_days_ago;
       
END;
$BODY

Can someone please help what is wrong in above, and should make expected result. I wanted to pass an array of string and in query.

Error I am getting is

LINE 2:  WHERE  (clbk_Typ::TEXT) IN (($1))  AND (current_date - cre_...
                                 ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.
QUERY:  SELECT * FROM dev.clbk_logs  
    WHERE  (clbk_Typ::TEXT) IN (($1))  AND (current_date - cre_dte::date)< p_days_ago
CONTEXT:  PL/pgSQL function dev.my_method(text[],integer) line 3 at RETURN QUERY
SQL state: 42883

1 Answers1

1

You need to use = any() not IN with an array.

The cast to ::text is also not needed. And for readability I would recommend using the parameter name, rather than the number:

CREATE OR REPLACE FUNCTION dev.my_method(p_callback_types TEXT[], p_days_ago INT) 
RETURNS SETOF dev.fourkites_clbk_logs 
    LANGUAGE plpgsql
AS $BODY$      
BEGIN
    return query
    SELECT * 
    FROM dev.fourkites_clbk_logs  
    WHERE clbk_Typ = any (p_callback_type)  
      AND (current_date - cre_dte::date) < p_days_ago;
END;
$BODY

Note that your condition on cre_dte can't use an index if you ever create one. If you want that condition to be able to use an index, change it to:

and cre_dte >= current_date - p_days_ago;
  • Thanks @a_horse_with_no_name But there is problem as the result looks like below "(194507,UPDATE,"{""Scac"": ""HLCU"", ""Tags"": [""0001311330""], ""Shipper"": ""mercedesbenz-usa"", ""Latitude"": ""41.0"", ""Location"": ""41.00, -32.30"", ""Timezone"": ""EU"", ""Longitude"": ""-32..7"", ""Timestamp"": ""2020-11-22T00:50:46+01:00"", ""LoadNumber"": ""HLCUS-BU 3082"", ""MessageType"": ""UPDATE"", ""TimezoneOffset"": 300, ""FourKitesLoadId"": 85061, ""ReferenceNumbers"": [""HLBU 3081422"", ""026627449""], ""TimezoneShortName"": ""CET""}","2020-11-22 00:01:37.274",85417061,"HLCUS-81422",NULL)" – Dnyaneshwar Jadhav Nov 23 '20 at 08:02
  • the result looks like in single column, they are not in different columns. what is missing – Dnyaneshwar Jadhav Nov 23 '20 at 08:05
  • 1
    that's a different question that has been answered e.g. here: https://stackoverflow.com/questions/49299858/ or https://stackoverflow.com/questions/14628771/postgres-function-returning-table-not-returning-data-in-columns –  Nov 23 '20 at 08:13
  • Yes, sorry for that I havent mentioned at the beginning itself. – Dnyaneshwar Jadhav Nov 23 '20 at 08:29
  • it works for me, and I am getting expected result from above block. thanks you – Dnyaneshwar Jadhav Nov 23 '20 at 08:30