简体   繁体   中英

How to pass a parameters in native query for stored procedure

I'm trying to pass parameters for stored procedure in native query, url is http://localhost:3025/bbr?BatteryBankID=1&fromDate=2017-01-10&toDate=2017-12-25

STORED PROCEDURE

Alter PROCEDURE [dbo].[Sample]
@BatteryBankID Int,
@fromDate DateTime,
@toDate DateTime

AS BEGIN

select 
bbr.StrVoltage,bbr.ChrgCurr
from BatteryBankReadings bbr
where bbr.BatteryBankID = @BatteryBankID 
and ReadTime between @fromDate and @toDate

END
GO

REPOSITORY

public interface SampleRepo extends CrudRepository<BatteryBankReadings, Integer> {


    @Query(value = "{call Sample :BatteryBankID, ':fromDate', ':toDate'}", nativeQuery = true)
    public List<BatteryBankReadings> samp(@Param("BatteryBankID") int BatteryBankID, @Param("fromDate") String fromDate,@Param("toDate") String toDate);
}

EXCEPTION:

java.lang.IllegalArgumentException: Parameter with that name [fromDate] did not exist
    at org.hibernate.jpa.spi.BaseQueryImpl.findParameterRegistration(BaseQueryImpl.java:487) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:638) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:163) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:32) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:139) ~[spring-data-jpa-1.11.9.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.StringQueryParameterBinder.bind(StringQueryParameterBinder.java:61) ~[spring-data-jpa-1.11.9.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:101) ~[spring-data-jpa-1.11.9.RELEASE.jar:na]
...........SO ON

Please do help me, Thanks in advance -:)

by keeping parameters within the parenthesis it works,

public interface SampleRepo extends CrudRepository<BatteryBankReadings, Integer> {

        @Query(value = "{call Sample (:BatteryBankID, :fromDate, :toDate)}", nativeQuery = true)
        public List<BatteryBankReadings> samp(@Param("BatteryBankID") int BatteryBankID, @Param("fromDate") String fromDate,@Param("toDate") String toDate);
    }

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