简体   繁体   中英

Error: Parameter out was not defined for stored procedure

When I call this stored procedure:

procedure [dbo].[pTest]
     @Date varchar(max) =null
as
begin
select * from test
end

from java using Spring Data JPA 1.10.2 with com.microsoft.sqlserver's sqljdbc 4.2. I get no error. But when I add another parameter to the procedure

procedure [dbo].[pTest]
     @ReportData varbinary(max) out
     ,@Date varchar(max) =null
as
begin
select * from test
end

I get the following error:

2016-10-06 15:50:16.283 DEBUG 32604 --- [           main] c.m.s.jdbc.internals.SQLServerException  : *** SQLException:SQLServerCallableStatement:9 com.microsoft.sqlserver.jdbc.SQLServerException: Parameter out was not defined for stored procedure pTest. Parameter out was not defined for stored procedure pTest.
2016-10-06 15:50:16.289 DEBUG 32604 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : Error preparing CallableStatement [pTest]

com.microsoft.sqlserver.jdbc.SQLServerException: Parameter out was not defined for stored procedure pTest.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191) ~[sqljdbc-4.2.jar:na]

If I remove the @Date parameter and modify the code accordingly, I get no error. Once I add back the @Date parameter to have two parameters, I get the error again. Why?

The code for two params is as follows:

@Entity
@NamedStoredProcedureQuery(name = "Test.getTest", procedureName = "pTest", parameters = {
        @StoredProcedureParameter(mode = ParameterMode.IN, name = "Date", type = String.class),
        @StoredProcedureParameter(mode = ParameterMode.OUT, name = "ReportData", type = byte[].class)
          })
public class Test {

    // serves no purpose other than to meet
    // JPA requirement
    @Id
    private byte[] reportData;
}

Spring data repository:

public interface TestRepository  extends Repository<Test, Long> {
    @Procedure("pTest")
    byte[] get(@Param("Date") String date);
}

Test code:

@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional(transactionManager="transactionManager")
public class TestRepositoryTest {

    @Autowired
    private TestRepository testRepository;

    @Test
    public void testGet() throws SQLException {

        byte[] blob = testRepository.get("hi");
        //error occurs
    }

}

Found the issue :) The attribute name of @NamedStoredProcedureQuery needs to match the repository method name. So change this:

@Entity
@NamedStoredProcedureQuery(name = "Test.getTest", procedureName = "pTest", parameters = {
        @StoredProcedureParameter(mode = ParameterMode.IN, name = "Date", type = String.class),
        @StoredProcedureParameter(mode = ParameterMode.OUT, name = "ReportData", type = byte[].class)
          })

to this:

@Entity
@NamedStoredProcedureQuery(name = "Test.get", procedureName = "pTest", parameters = {
        @StoredProcedureParameter(mode = ParameterMode.IN, name = "Date", type = String.class),
        @StoredProcedureParameter(mode = ParameterMode.OUT, name = "ReportData", type = byte[].class)
          })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM