Postgres function input parameter as TEXT Array problem - postgresql

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

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;

Related

Postgresql - Returned type record does not match expected type uuid

I have very simple function that should return few rows including the uuid type as the first column.
Function:
CREATE OR REPLACE FUNCTION public.export_wizard()
RETURNS TABLE(id uuid, my_column text)
LANGUAGE plpgsql
AS $function$
BEGIN
return query select (w.id, w.my_column) from wizard w;
END;
$function$
;
Table (short version):
CREATE TABLE IF NOT EXISTS public.wizard
(
id uuid NOT NULL,
my_column text COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT wizard_pkey PRIMARY KEY (id)
)
After calling the function like this: select * from export_wizard();
I got an error:
SQL Error [42804]: ERROR: structure of query does not match function result type
Detail: Returned type record does not match expected type uuid in column 1.
Where: PL/pgSQL function export_wizard() line 3 at RETURN QUERY
Thanks for any advices.
I made terrible mistake by adding (no idea why) brackets in SELECT statement as mentioned by #Adrian Klaver.
Correct: return query select w.id, w.my_column from wizard w;

query has no destination for result data i am facing this error

PostgreSQL 14
TABLE
CREATE TABLE IF NOT EXISTS settings.tbl_tmp
(
pk_sys_qr_settings_id bigint NOT NULL DEFAULT nextval('settings.tbl_tmp_pk_sys_qr_settings_id_seq'::regclass),
sin_product smallint DEFAULT 0,
vhr_key character varying(10) COLLATE pg_catalog."default" NOT NULL,
vhr_value character varying(250) COLLATE pg_catalog."default",
CONSTRAINT tbl_tmp_pkey PRIMARY KEY (pk_sys_qr_settings_id),
CONSTRAINT tbl_tmp_vhr_key_key UNIQUE (vhr_key)
)
--FUNCTION 1 - CALL FROM fn_test
CREATE OR REPLACE FUNCTION fn_test1(sinProduct SMALLINT,
vhrKey VARCHAR(10),
vhrValue VARCHAR(250))
RETURNS VOID AS $$
BEGIN
INSERT INTO settings.tbl_tmp
(sin_product, vhr_key, vhr_value)
VALUES
(sinProduct, vhrKey, vhrValue);
END;
$$ LANGUAGE plpgsql;
FUNCTION 2
CREATE OR REPLACE FUNCTION fn_test(sinProduct SMALLINT,
vhrKey VARCHAR(10),
vhrValue VARCHAR(250))
RETURNS VOID AS $$
DECLARE
a SMALLINT;
BEGIN
BEGIN
SELECT * FROM fn_test1(1::SMALLINT, 'Anil-5'::VARCHAR(10), 'KV-5'::VARCHAR(250));
INSERT INTO settings.tbl_tmp
(sin_product, vhr_key, vhr_value)
VALUES
(sinProduct, vhrKey, vhrValue);
END;
END;
$$ LANGUAGE plpgsql;
--ERROR
ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT: PL/pgSQL function fn_test(smallint,character varying,character varying) line 7 at SQL statement
SQL state: 42601
Quote from the manual
Sometimes it is useful to evaluate an expression or SELECT query but discard the result, for example when calling a function that has side-effects but no useful result value. To do this in PL/pgSQL, use the PERFORM statement:
As the called function doesn't return anything to begin with, it's safe to use perform.
So use:
perform fn_test1(1::SMALLINT, 'Anil-5'::VARCHAR(10), 'KV-5'::VARCHAR(250));
Instead of select * from fn_test(..)
As you are using Postgres 14, you could also use a procedure in both cases, as you don't want to return a result.

function does not exists in postgreSQL .. Why ?

Need your help please , can't understand why i got the following error , i am not a professional postgresql developer ..
As you can see the function created , so why the function not exist occurred ?
create or replace function loginAttempt (u_email character varying, u_password character varying, date_time timestamptz, OUT attempt smallint) returns smallint AS $$
BEGIN
INSERT INTO login_attempts (typed_password, date_time, attempt_nu, email) VALUES (u_password, date_time, attempt_nu, email);
IF attempt = 3 THEN INSERT INTO warnings (u_email,u_password) VALUES (u_email,u_password);
END IF;
END;
$$ LANGUAGE plpgsql;
select loginattempt ('Jon.Jones88#gmail.com','+_#kjhfdb987', now(), 1);
ERROR: function loginattempt(unknown, unknown, timestamp with time zone, integer) does not exist
LINE 1: select loginattempt ('Jon.Jones88#gmail.com','+_#kjhfdb987',...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 8
You have defined the last parameter as an OUT parameter, that means you can't pass a value for it.
You need to use:
select loginattempt ('Jon.Jones88#gmail.com','+_#kjhfdb987', now());
As you are not writing to the parameter attempts I don't see a reason to define it as an out parameter to begin with. You can simply return the value if you need it:
create or replace function loginAttempt (u_email character varying, u_password character varying, u_date_time timestamptz, u_attempt smallint)
returns smallint
AS $$
BEGIN
INSERT INTO login_attempts (typed_password, date_time, attempt_nu, email)
VALUES (u_password, u_date_time, u_attempt, u_email);
IF u_attempt = 3 THEN
INSERT INTO warnings (u_email,u_password) VALUES (u_email,u_password);
END IF;
return u_attempt;
END;
$$ LANGUAGE plpgsql;
As the value 1 is assumed to be an integer, you need to cast that value when calling the function:
select loginattempt ('Jon.Jones88#gmail.com','+_#kjhfdb987', now(), 1::smallint);
Online example: https://rextester.com/YNIQ55561

catching select query return value in postgresql function and use it

I want to execute this function. But it got error said
ERROR:
syntax error at or near ":="
LINE 7: select result:=MAX(path_history_id)as path INTO result from...
In this function I want to:
execute select with (MAX) and it will return maximum id from a table;
catch that value (it is an integer value);
put that value into last select query where condition.
I cant find a way in postgresql to do this.
CREATE OR REPLACE FUNCTION memcache(IN starting_point_p1 character varying, IN ending_point_p1 character varying)
RETURNS TABLE(path integer, movement_id_out integer, object_id_fk_out integer, path_history_id_fk_out integer, walking_distance_out real, angel_out real, direction_out character varying, time_stamp_out timestamp without time zone, x_coordinate_out real, y_coordinate_out real, z_coordinate_out real) AS
$BODY$
DECLARE result int;
BEGIN
select result:=MAX(path_history_id)as path INTO result from path_history_info where starting_point=starting_point_p1 and ending_point =ending_point_p1 and achieve='1';
return query
select * from movement_info where path_history_id_fk=result;
END;
$BODY$
LANGUAGE plpgsql
Syntax Error
The first query inside your function needs to be changed as follows:
select MAX(path_history_id)as path INTO result
from path_history_info
where starting_point=starting_point_p1
and ending_point =ending_point_p1 and achieve='1';
A single Query
You don't actually need a stored procedure for this. A single query can achieve the same result.
select * from movement_info where path_history_id_fk =
(SELECT MAX(path_history_id) FROM path_history_info
where starting_point=starting_point_p1
and ending_point =ending_point_p1 and achieve='1';

how to convert a varchar field value into an integer field

for example in a table one field is character varying so how to convert that character varying to an Integer type, what exactly is I need to get the return value in Integer datatype
Database : postgresql-9.2
i would suggest to make Functions to do the job i.e
CREATE OR REPLACE FUNCTION chartoint(charparam character varying)
RETURNS integer AS
$BODY$
SELECT CASE WHEN trim($1) ~ '[0-9]+' THEN CAST(trim($1) AS integer) ELSE NULL END;
$BODY$
LANGUAGE sql IMMUTABLE STRICT
You may try like this:
SELECT NULLIF(your_value, '')::int
or extend that like thats
SELECT CAST(coalesce(column_name, '0') AS integer) as Value
from table_name
SELECT mycolumn::integer FROM mytable WHERE something
SELECT CASE WHEN mycolumn = NULL THEN NULL ELSE mycolumn :: Integer END
FROM mytable WHERE something