使用 @Query 调用存储过程会导致异常:调用 ProcedureName 时参数的数量或类型错误



我在调用存储过程时遇到问题

create or replace PROCEDURE TProc1 
(i_cob_date IN   DATE,
i_location IN   VARCHAR2,
o_ret      OUT  VARCHAR2
)
AS
BEGIN
--logic to update a table based on i_cob_date 
o_ret := '0';
commit;
END TProc1;
public interface MyRepository extends CrudRepository<MyEntity, Long> {
@Query(value = "call TProc1(:i_cob_date, :i_location)", nativeQuery = true)
String markStatus(@Param("i_cob_date")Date cobDate, @Param("i_location")String location);
}

我正在使用带有 Spring Data JPA 的 SpringBoot,当我尝试调用我得到以下错误的方法时。

Could not extract the ResultSet
Caused by: java.sql.SQLException: ORA-06553: PLS-306: wrong number or types of arguments in call to 'TProc1'

您已使用三个参数声明了存储过程,但在 SQL 脚本的@Query注释中仅使用两个参数调用它。

修复后,您将在下一个问题中运行,因为脚本不返回值。JPA不知道从中返回什么,因此Spring Data JPA也是如此。我完全不确定这是否可以用您选择的方法解决。

对于使用 JPA 调用存储过程,我强烈建议使用@NamedStoredProcedureQuery声明它们。 然后,您可以使用 Spring Data JPA 访问该存储过程。

最新更新