首页 > 解决方案 > plpgsql:没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换

问题描述

我使用 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());

可能是什么问题?

我对此完全陌生。

更新:

过程调用:

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

错误:

SQL 错误 [42883]:错误:函数 public.proc_insert_test(整数,字符变化,字符变化,字符,字符,带时区的时间戳,整数,整数,整数,带时区的时间戳)不存在提示:没有函数匹配给定名称和参数类型。您可能需要添加显式类型转换。职位:8

程序 过程调用

标签: postgresqlplpgsql

解决方案


Postgres 不允许隐式转换timestampdate数据类型。注意 - Postgresdate类型与 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的转换正在丢失转换。默认情况下是不允许的。所以你应该强制它(通过显式转换),或者你应该使用current_date返回date类型的伪常量,并且不需要任何转换。


推荐阅读