procedure myProcedure (text, text) does not exist Hint: No procedure matches the given name and argument types

huangapple go评论162阅读模式
英文:

procedure myProcedure (text, text) does not exist Hint: No procedure matches the given name and argument types

问题

无论何时尝试从我的Java应用程序中调用PostgreSQL 11.4中的任何存储过程,都会出现此问题:过程pkg$my_procedure(text, text)不存在。请注意,我可以从数据库中调用该存储过程。

我正在使用PostgreSQL JDBC版本42.2.16。

存储过程声明:

create procedure pkg$my_procedure(i_param_name text, i_param_2 text, INOUT o_object refcursor)
    language plpgsql
as
$$

BEGIN
// myLogic

调用存储过程的Java代码:

Connection con = null;
CallableStatement callableStatement = null;
ResultSet rs = null;
Object obj = null;
try {
    con = eRestaurantConnection.getConnetion();
    callableStatement = con.prepareCall("call pkg$my_procedure(cast(? as text),cast(? as text),?)");
    callableStatement.setString(1, string1);
    callableStatement.setString(2, string2);
    callableStatement.registerOutParameter(3, Types.REF_CURSOR);
    callableStatement.execute();
    rs = (ResultSet) callableStatement.getObject(3);
    obj = fillObjectInfo(rs);
} catch (Exception ex) {
    LOG.error(ex.getLocalizedMessage(), ex);
} finally {
    if (rs != null)
        rs.close();
    if(callableStatement!=null)callableStatement.close();
    if(con!=null)con.close();
}
return obj;

异常信息:

ERROR: 过程pkg$my_procedure(text, text)不存在
  提示: 没有与给定名称和参数类型匹配的过程。您可能需要添加显式的类型转换。
  位置: 6
org.postgresql.util.PSQLException: ERROR: 过程pkg$my_procedure(text, text)不存在
  提示: 没有与给定名称和参数类型匹配的过程。您可能需要添加显式的类型转换。
  位置: 6
    ...

从PostgreSQL中的调用:

do $$
declare
    result refcursor = 'generated_result_cursor';
    rec record;
begin
    open result for call pkg$my_procedure(i_param_name  := 'name', i_param_2 := 'param', o_object := null);
    LOOP
        FETCH from result into rec;
        EXIT WHEN NOT FOUND;
        raise notice 're: %',rec;
        EXIT;
    END LOOP;
end
$$;
英文:

whenever trying calling any stored procedure in PostgreSQL 11.4 from my java application but getting this issue procedure pkg$my_procedure(text, text) does not exist. Note that I'm able to call the SP from DB.

im using PostgreSQL JDBC version 42.2.16

the SP declaration

create procedure pkg$my_procedure(i_param_name text, i_param_2 text, INOUT o_object refcursor)
    language plpgsql
as
$$

BEGIN
// myLogic

Java code to call the SP

        Connection con = null;
        CallableStatement callableStatement = null;
        ResultSet rs = null;
        Object obj = null;
        try {
            con = eRestaurantConnection.getConnetion();
            callableStatement = con.prepareCall("call pkg$my_procedure(cast(? as text),cast(? as text),?)");
            callableStatement.setString(1, string1);
            callableStatement.setString(2, string2);
            callableStatement.registerOutParameter(3, Types.REF_CURSOR);
            callableStatement.execute();
            rs = (ResultSet) callableStatement.getObject(3);
            obj = fillObjectInfo(rs);
        } catch (Exception ex) {
            LOG.error(ex.getLocalizedMessage(), ex);
        } finally {
            if (rs != null)
                rs.close();
            if(callableStatement!=null)callableStatement.close();
            if(con!=null)con.close();
        }
        return obj;

the Exception

ERROR: procedure pkg$my_procedure(text, text) does not exist
  Hint: No procedure matches the given name and argument types. You might need to add explicit type casts.
  Position: 6
org.postgresql.util.PSQLException: ERROR: procedure pkg$my_procedure(text, text) does not exist
  Hint: No procedure matches the given name and argument types. You might need to add explicit type casts.
  Position: 6
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2553)
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2285)
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:323)
	at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:473)
	at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:393)
	at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:164)
	at org.postgresql.jdbc.PgCallableStatement.executeWithFlags(PgCallableStatement.java:83)
	at org.postgresql.jdbc.PgPreparedStatement.execute(PgPreparedStatement.java:153)
	at com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44)
	at com.zaxxer.hikari.pool.HikariProxyCallableStatement.execute(HikariProxyCallableStatement.java)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at net.ttddyy.dsproxy.proxy.StatementProxyLogic.performQueryExecutionListener(StatementProxyLogic.java:310)
	at net.ttddyy.dsproxy.proxy.StatementProxyLogic.access$700(StatementProxyLogic.java:36)
	at net.ttddyy.dsproxy.proxy.StatementProxyLogic$1.execute(StatementProxyLogic.java:122)
	at net.ttddyy.dsproxy.listener.MethodExecutionListenerUtils.invoke(MethodExecutionListenerUtils.java:41)
	at net.ttddyy.dsproxy.proxy.StatementProxyLogic.invoke(StatementProxyLogic.java:119)
	at net.ttddyy.dsproxy.proxy.jdk.CallableStatementInvocationHandler.invoke(CallableStatementInvocationHandler.java:36)
	...

the call from PostgreSQL

do $$
declare
	result refcursor = 'generated_result_cursor';
    rec record;
begin
	open result for call pkg$my_procedure(i_param_name  := 'name', i_param_2 := 'param', o_object := null);
    LOOP
        FETCH from result into rec;
        EXIT WHEN NOT FOUND;
        raise notice 're: %',rec;
        EXIT;
    END LOOP;
end
$$;

答案1

得分: 2

你创建了存储过程而没有给名称加双引号,所以它被存储为小写。

错误信息报告了一个包含大写字母的函数名。由于PG区分大小写,无法找到存储过程。

--> 使用小写函数名

callableStatement = con.prepareCall("call myprocedure(cast(? as text),cast(? as text),?)");
英文:

You created the stored procedure without doublequoting the name, so it is stored in lowercase.

The error message reports a functioname containing an uppercase. Since PG is case sensitive, the stored procedure is not found.

--> use a lower case function name

callableStatement = con.prepareCall("call myprocedure(cast(? as text),cast(? as text),?)");

答案2

得分: 1

以下是翻译好的部分:

所有这些地方都需要在上述代码中进行修复。感谢 @JGH 发现了第一个问题。

  1. 您需要确保大小写匹配。
  2. 即使参数用于输出,也需要设置所有参数。如果有输出需要将它们设置为 null。
  3. 最后要做的是禁用自动提交。

调用存储过程的 Java 代码

        Connection con = null;
        con.setAutoCommit(false);
        CallableStatement callableStatement = null;
        ResultSet rs = null;
        Object obj = null;
        try {
            con = eRestaurantConnection.getConnetion();
            callableStatement = con.prepareCall("call pkg$my_procedure(cast(? as text),cast(? as text),?)");
            callableStatement.setString(1, string1);
            callableStatement.setString(2, string2);
            callableStatement.setNull(3,  Types.OTHER);
            callableStatement.registerOutParameter(3, Types.REF_CURSOR);
            callableStatement.execute();
            rs = (ResultSet) callableStatement.getObject(3);
            obj = fillObjectInfo(rs);
        } catch (Exception ex) {
            LOG.error(ex.getLocalizedMessage(), ex);
        } finally {
            if (rs != null)
                rs.close();
            if(callableStatement!=null)callableStatement.close();
            if(con!=null)con.close();
        }
        return obj;
英文:

all this point needs to be fixed in the above code. Thanks for @JGH that find the first issue.

  1. you need to make sure that the case matches.
  2. need to set the all param even if they are used for output. in case there is an output need to set them as null.
  3. The last thing to do need to disable the auto-commit.

Java code to call the SP

        Connection con = null;
        con.setAutoCommit(false);
        CallableStatement callableStatement = null;
        ResultSet rs = null;
        Object obj = null;
        try {
            con = eRestaurantConnection.getConnetion();
            callableStatement = con.prepareCall("call pkg$my_procedure(cast(? as text),cast(? as text),?)");
            callableStatement.setString(1, string1);
            callableStatement.setString(2, string2);
            callableStatement.setNull(3,  Types.OTHER);
            callableStatement.registerOutParameter(3, Types.REF_CURSOR);
            callableStatement.execute();
            rs = (ResultSet) callableStatement.getObject(3);
            obj = fillObjectInfo(rs);
        } catch (Exception ex) {
            LOG.error(ex.getLocalizedMessage(), ex);
        } finally {
            if (rs != null)
                rs.close();
            if(callableStatement!=null)callableStatement.close();
            if(con!=null)con.close();
        }
        return obj;

huangapple
  • 本文由 发表于 2020年10月27日 23:01:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64557175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定