Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve a error 42883 in Postgres?

When I run this script:

CREATE OR REPLACE FUNCTION registeruser(character varying, character varying, money, character varying, character varying, date)
  RETURNS void AS
$BODY$
BEGIN
INSERT INTO "Users"("Name", "Surnames", "Money", "Email", "Password", "UserType", "State", "BirthDate")
VALUES ($1,$2,$3,$4,$5,'Costumer','Active',$6);

END;
$BODY$  LANGUAGE plpgsql VOLATILE;

SELECT RegisterUser('Andrea','Gianchero',0,'[email protected]','rome','1960-04-12');

I get this error:

ERROR:  function registeruser(unknown, unknown, integer, unknown, unknown, unknown) does not exist
LINE 1: SELECT RegisterUser('Andrea','Gianchero',0,'[email protected]...
             ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

How to fix this?

like image 847
Francisco Avatar asked Sep 20 '25 18:09

Francisco


1 Answers

A string literal such as 'Andrea' is untyped and is not the same as a parameter of type varchar. Test the following two lines in your psql console:

select 'Andrea';
select 'Andrea'::character varying;

The first will output Andrea with default column name ?column? the second will output Andrea but with column name varchar. From the documentation:

The notations varchar(n) and char(n) are aliases for character varying(n) and character(n), respectively.

To solve your problem, simply cast all the string literals in your function call to type varchar (or character varying). Also, the 0 should be cast to type money, at the moment it is recognized as an integer:

SELECT RegisterUser('Andrea'::varchar, 'Gianchero'::varchar, 0::money,...)
like image 152
apotry Avatar answered Sep 22 '25 16:09

apotry