postgresql plpgsql:没有函数匹配给定的名称和参数类型,您可能需要添加显式类型转换

xxe27gdn  于 5个月前  发布在  PostgreSQL
关注(0)|答案(2)|浏览(129)

我使用DBeaver在PostgreSQL中创建了一个函数。&我试图通过从DBeaver调用该函数将数据插入表中。但它给了我一个错误:
SQL错误[42883]:错误:函数public.proc_insert_test(integer,unknown,unknown,unknown,unknown,timestamp with time zone,integer,integer,integer,timestamp with time zone)不存在提示:没有函数与给定的名称和参数类型匹配。您可能需要添加显式类型转换。位置:8
功能说明:

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$
;

字符串
调用函数:

select public.proc_insert_test(123, 'Test2', 'Test2', 'T', 'T', now(), 1, 9, 1234, now());


有什么问题吗?
我完全是新手。

6ojccjat

6ojccjat1#

Postgres不允许从timestampdate数据类型的隐式转换。注意- Postgres date类型与Oracle的date类型不同。

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)

字符串
timestampnow()函数的结果类型)到date的转换会丢失转换。默认情况下是不允许的。所以你应该强制执行(通过显式转换),或者你应该使用返回date类型的伪常量current_date,并且不需要任何转换。

lzfw57am

lzfw57am2#

我使用OUT参数创建了PL/pgSQL function,如下所示:

CREATE FUNCTION my_func(OUT value INTEGER) AS $$
BEGIN                 -- ↑ Here
SELECT 2 + value INTO value;
END;
$$ LANGUAGE plpgsql;

字符串
然后,调用my_func(3)得到相同的错误,如下所示:

postgres=# SELECT my_func(3);
ERROR:  function my_func(integer) does not exist
LINE 1: SELECT my_func(3);
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.


因此,我使用了INOUT参数,如下所示:

CREATE FUNCTION my_func(INOUT value INTEGER) AS $$
BEGIN                  -- ↑ Here
SELECT 2 + value INTO value;
END;
$$ LANGUAGE plpgsql;


然后,我可以调用my_func(3)而不会出错,如下所示:

postgres=# SELECT my_func(3);
 my_func
---------
       5
(1 row)

相关问题