Postgresql syntex error for function declaration with default argument - postgresql

Sorry, noob question. Therefore I will keep it short. Code, error message and specific part of code line are below.
Many thanks.
DROP FUNCTION IF EXISTS pfi_pilot.fr_GetCrossTrades(IN par_from_dt TIMESTAMP WITHOUT TIME ZONE, IN par_to_dt TIMESTAMP WITHOUT TIME ZONE, IN par_asset_classes VARCHAR(255) DEFAULT 'ALL', IN par_debug CHAR(255) DEFAULT 'N' NOT NULL, IN return_code int DEFAULT NULL, IN p_refcur refcursor DEFAULT NULL, IN p_refcur_2 refcursor DEFAULT NULL, IN p_refcur_3 refcursor DEFAULT NULL, IN p_refcur_4 refcursor DEFAULT NULL, IN p_refcur_5 refcursor DEFAULT NULL);
CREATE OR REPLACE FUNCTION pfi_pilot.fr_GetCrossTrades(IN par_from_dt TIMESTAMP WITHOUT TIME ZONE, IN par_to_dt TIMESTAMP WITHOUT TIME ZONE, IN par_asset_classes VARCHAR(255) DEFAULT 'ALL', IN par_debug CHAR(255) DEFAULT 'N' is NOT NULL, IN return_code int DEFAULT NULL, IN p_refcur refcursor DEFAULT NULL, IN p_refcur_2 refcursor DEFAULT NULL, in p_refcur_3 refcursor DEFAULT NULL, in p_refcur_4 refcursor DEFAULT NULL, in p_refcur_5 refcursor DEFAULT NULL)
Error message:
SQL Error [42601]: ERROR: syntax error at or near "DEFAULT"
Position: 173
at:
IN par_asset_classes VARCHAR(255) DEFAULT 'ALL'

You don't need the parameters for the DROP FUNCTION if you have not overloaded your function:
DROP FUNCTION IF EXISTS pfi_pilot.fr_GetCrossTrades;
If you did overload the function, you should only list the data types (without the parameter name, mode or default value):
DROP FUNCTION IF EXISTS pfi_pilot.fr_GetCrossTrades(
TIMESTAMP WITHOUT TIME ZONE,
TIMESTAMP WITHOUT TIME ZONE,
VARCHAR(255),
CHAR(255),
int,
refcursor, refcursor, refcursor, refcursor, refcursor);
Note that use of the char data type is discouraged.
There is also no real need to use varchar(n) for a parameter, using text for both parameters would make your life a lot easier.

Related

plpgsql: No function matches the given name and argument types. You might need to add explicit type casts

I've created a stored procedure in PostgreSQL using DBeaver.
& I'm trying to insert data into table by calling the procedure from DBeaver.
But it's giving me an error
SQL Error [42883]: ERROR: function public.proc_insert_test(integer, unknown, unknown, unknown, unknown, timestamp with time zone, integer, integer, integer, timestamp with time zone) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Position: 8
Procedure:
CREATE OR REPLACE FUNCTION public.proc_insert_test(p_brndcode integer,
p_brndname varchar(100),
p_brndsname varchar(100),
p_prdtype char(1),
p_discontinue char(1),
p_crddate date,
p_status integer,
p_recstat integer,
p_brndgrpseqno integer,
p_wefrom date)
RETURNS char
LANGUAGE plpgsql
AS $body$
BEGIN
Insert into arc_mmstbrndgroup(brndcode, brndname, brndsname, prdtype, discontinue, crddate, status, recstat, brndgrpseqno, wefrom)
values(p_brndcode, p_brndname, p_brndsname, p_prdtype, p_discontinue, p_crddate, p_status, p_recstat, p_brndgrpseqno, p_wefrom);
END;
$body$
;
Calling the procedure:
select public.proc_insert_test(123, 'Test2', 'Test2', 'T', 'T', now(), 1, 9, 1234, now());
What can be the issue?
I'm totally new to this.
Update:
Procedure calling:
select public.proc_insert_test(123, 'Test2'::varchar(100), 'Test2'::varchar(100), 'T'::char(1), 'T'::char(1), now(), 1, 9, 1234, now());
Error:
SQL Error [42883]: ERROR: function public.proc_insert_test(integer, character varying, character varying, character, character, timestamp with time zone, integer, integer, integer, timestamp with time zone) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Position: 8
Postgres doesn't allow implicit conversion from timestamp to date data type. Attention - Postgres date type is different from Oracle's date type.
CREATE OR REPLACE FUNCTION public.test(v date)
RETURNS void
LANGUAGE plpgsql
AS $function$
BEGIN
RAISE NOTICE '%', v;
END;
$function$
postgres=# SELECT test(now());
ERROR: function test(timestamp with time zone) does not exist
LINE 1: SELECT test(now());
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
postgres=# SELECT test(current_date);
NOTICE: 2019-11-14
+------+
| test |
+------+
| |
+------+
(1 row)
postgres=# SELECT test(now()::date);
NOTICE: 2019-11-14
+------+
| test |
+------+
| |
+------+
(1 row)
The conversion from timestamp (result type of now() function) to date is losing conversions. It is not allowed by default. So you should to enforce it (by explicit casting), or you should to use pseudo constant current_date that returns date type, and there is not necessary any conversion.

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

Postgresql return table result

I want to return table. But Here is the error I am getting. How can I solve this one to return a table?
ERROR: return type mismatch in function declared to return record
DETAIL: Function's final statement must be SELECT or INSERT/UPDATE/DELETE RETURNING.
CONTEXT: SQL function "delete"
********** Error **********
ERROR: return type mismatch in function declared to return record
SQL state: 42P13"
CREATE TABLE myTable (
id serial NOT NULL,
name varchar(255) default NULL,
surname varchar(255) default NULL,
phone varchar(100) default NULL,
constraint p_key primary key(id)
) ;
CREATE OR REPLACE FUNCTION delete(name text,surname text)
RETURNS table(id int, name text, surname text, phone text) AS
$$
DELETE FROM myTable where name=$1 and surname=$2;
$$
LANGUAGE 'sql' VOLATILE;

Insert Function Postgresql how to test with parameters?

I have the following insert function in postgresql 9.4.
I am totally unable to test it with parameters.
DROP FUNCTION "Public".cash_inserts(integer, bigint, timestamp without time zone, character varying, numeric, numeric);
CREATE OR REPLACE FUNCTION "Public".cash_inserts(
cashid integer,
cashserial bigint,
cashdate timestamp without time zone,
cashmemo text,
cashcredit numeric,
cashdebit numeric)
RETURNS integer AS
$BODY$
INSERT INTO "Public"."CashAccounts"
VALUES
(
CashId,
CashSerial,
CashDate,
CashMemo,
CashCredit,
CashDebit
) RETURNING CashId ;
$BODY$
LANGUAGE sql VOLATILE
COST 100;
ALTER FUNCTION "Public".cash_inserts(integer, bigint, timestamp without time zone, text, numeric, numeric)
OWNER TO robert100;
Because the fields 'cashid,cashserial,cashdate' are either auto-incrementing or created by the database, I did not enter any values. However, Cashmemo is'Not NUll' and 'Cashcredit,Cashdebit' are nullable or optional.
Why include cashid,cashserial,cashdate in the function insert statement if they are either auto-incrementing or created by the database as you said and the function returns int?
Maybe try:
DROP FUNCTION "Public".cash_inserts(integer, bigint, timestamp without time zone, character varying, numeric, numeric);
CREATE OR REPLACE FUNCTION "Public".cash_inserts(
cashmemo text,
cashcredit numeric,
cashdebit numeric)
RETURNS integer AS
$BODY$ INSERT INTO "Public"."CashAccounts"
VALUES
(
CashMemo,
CashCredit,
CashDebit
) RETURNING CashId ; $BODY$
LANGUAGE sql VOLATILE
COST 100;
ALTER FUNCTION "Public".cash_inserts(text, numeric, numeric)
OWNER TO robert100
Then call the function with the variable parameters and cashid, cashserial and cashdate will be auto generated by the database.
It appears my original question was correct.What I discovered is that when you supply parameters, they must all be in quotes like this:
SELECT "Public".cash_inserts(
'10',
'4',
'12-12-12',
'COOL',
'200',
'300'
);

How to execute a stored procedure directly in postgresql?

I have created a procedure like :
CREATE OR REPLACE FUNCTION insert_user_ax_register(
user_name character varying(50),
password character varying(300),
role_id character varying(10),
created_dt date,
status boolean,
email character varying(50),
join_date character varying(30),
phone_no bigint,
client_address character varying(200),
full_name character varying(100),
financial_year character varying(10))
RETURNS void
AS $BODY$
declare
begin
INSERT INTO ax_register(user_name,password,role_id,created_dt,status,email,join_date,phone_no,client_address,full_name,financial_year)
VALUES (user_name,password,role_id,now(),true,email,join_date,phone_no,client_address,full_name,financial_year);
end
$BODY$
LANGUAGE plpgsql VOLATILE
and tried to execute it like this:
SELECT * from insert_user_ax_register('debasrita','debasrita','client001',now(),'t','abc#gmail.com',now(),'ctc','debasrita','2014-15',9090909090);
but it throws the following error :
ERROR: function insert_user_ax_register(unknown, unknown, unknown, timestamp with time zone, unknown, unknown, timestamp with time zone, unknown, unknown, unknown, bigint) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 16
Please help me out on this. I am new to pgsql and not able to find out any solution from google. I am using pgsql 9.1.3
May I know what is the correct way to achieve my objective?
The error message tells you what you need to be looking for:
"No function matches the given name and argument types"
As the function name seems correct, it can only be the parameters you are passing. So write down which value is passed for which parameter:
'debasrita' --> user_name character varying(50)
'debasrita' --> password character varying(300)
'client001' --> role_id character varying(10)
created_dt date --> now()
status boolean, --> 't'
email varchar(50) --> 'abc#gmail.com'
join_date varchar(30) --> now() << first error: now() is not a character constant
phone_no bigint --> 'ctc' << second error: 'ctc' is not a bigint
client_address varchar(200) --> 'debasrita'
full_name varchar(100) --> '2014-15'
financial_year varchar(10) --> 9090909090 << third error: 9090909090 is not a character literal
So you need to either adjust the parameter types, e.g. define join_date as date, not as varchar or adjust the values that you pass for each parameter.
And finally you need to call the function like this:
SELECT insert_user_ax_register(...);
rather than select * from ...
If you are using pgAdmintool just right click on the function or Stored Proc under a schema and select properties and then paramaeters .Now insert the value which you wannna insert.