调用一个 returns 没有的存储函数

Calling a stored function that returns nothing

我有这个功能,它只插入一些数据,returns什么都没有

CREATE OR REPLACE FUNCTION test(srcDc integer, targetDc integer)

RETURNS void AS

$BODY$

BEGIN

INSERT INTO configurable_code(key_column,customer_discovery_scope_id)
SELECT key_column, targetDc FROM configurable_code WHERE customer_discovery_scope_id=srcDc
AND KEY_Column NOT IN (SELECT KEY_Column FROM configurable_code where customer_discovery_scope_id in( targetDc));

INSERT INTO user_customer_scope ( user_id, customer_discovery_scope_id )
SELECT user_id,targetDc FROM user_customer_scope where customer_discovery_scope_id=srcDc
AND customer_discovery_scope_id NOT IN (SELECT customer_discovery_scope_id FROM user_customer_scope where customer_discovery_scope_id = targetDc );

INSERT INTO user_scope_license ( user_id, customer_scope_id, license )
SELECT user_id,targetDc, license FROM user_scope_license WHERE customer_scope_id=srcDc
AND customer_scope_id NOT IN (SELECT customer_scope_id FROM user_scope_license where customer_scope_id = targetDc );

END;

$BODY$

LANGUAGE 'plpgsql'

COST 100;

而且Postgres版本是10,不支持程序。所以我使用 JDBCTemplate 进行简单的调用

jdbcTemplate.update("select test(?,?)",srcDc,targetDc);

但是当 运行 它

我得到这个错误
2022-02-02 17:03:22,640 [http-nio-8090-exec-4] WARN  org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 0100E
2022-02-02 17:03:22,641 [http-nio-8090-exec-4] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - A result was returned when none was expected.
org.hibernate.exception.GenericJDBCException: A result was returned when none was expected.

它似乎可以与程序一起使用,但我不能使用它,因为版本是 10。SimpleJDBCCall 也不起作用。我不能使用 queryForObject,因为它需要一个 RowMapper。更好的方法是什么?

我在 the documentation 中找到这个:

The escapeSyntaxCallMode connection property controls how the driver transforms the call syntax to invoke functions or procedures.

The default mode, select, supports backwards compatibility for existing applications and supports function invocation only. This is required to invoke a void returning function.

因此您可以将其称为存储过程。这是使用 JDBC:

的完整代码示例
Class.forName("org.postgresql.Driver");

java.sql.Connection conn =
    java.sql.DriverManager.getConnection(
        "jdbc:postgresql://127.0.0.1/test?user=laurenz&escapeSyntaxCallMode=select"
    );

java.sql.CallableStatement stmt = conn.prepareCall("{ call mumble(?) }");
stmt.setInt(1, 42);
stmt.execute();
stmt.close();

conn.close();