如何解决Postgres中的错误42883?

时间:2016-02-03 21:53:22

标签: postgresql types casting plpgsql

当我运行此脚本时:

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,'gian@hotmail.com','rome','1960-04-12');

我收到此错误:

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

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

'Andrea'等字符串文字是无类型的,varchar类型的参数相同。在psql控制台中测试以下两行:

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

第一个会输出Andrea,其默认列名为?column? 第二个将输出Andrea,但列名为varchar。来自文档:

  

符号varchar(n)和char(n)分别是字符变化(n)和字符(n)的别名。

要解决您的问题,只需将函数调用中的所有字符串文字强制转换为varchar(或character varying)类型。此外,0应该投放到money类型,此时它被识别为integer

SELECT RegisterUser('Andrea'::varchar, 'Gianchero'::varchar, 0::money,...)