首页 > 解决方案 > 如何使用 Spring SimpleJdbcCall 调用 PostgreSQL 存储过程并从返回的 refcursor 获取数据

问题描述

我在 PostgreSQL 中有一个存储过程,它返回一个refcursor,我想使用 Spring Jdbc SimpleJdbcCall 调用它。

程序代码如下:

CREATE OR REPLACE FUNCTION show_emps() RETURNS refcursor AS $$
DECLARE
  ref refcursor;                                     -- Declare a cursor variable
BEGIN
  OPEN ref FOR SELECT emp_id, first_name FROM emp;   -- Open a cursor
  RETURN ref;                                                      
END;
$$ LANGUAGE plpgsql;

谁能帮我编写代码来调用此过程并使用 SimpleJdbcCall 类获取其数据。我正在使用以下代码进行调用,但它不起作用:

SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate);
jdbcCall.withProcedureName("show_emps");

Map<String, Object> result = jdbcCall.execute();

标签: postgresqlstored-proceduresspring-jdbcsimplejdbccall

解决方案


推荐阅读