Select Top 1 records from MS SQL using Spring Data JPA

Vijai :

I am using the below @Query annotation to get the first few record from MS-SQL. It's showing error saying "< operator > or AS expected..."

   @Query("SELECT Top 1 * FROM NEVS010_VEH_ACTV_COMMAND C WHERE C.EVS014_VIN = :vin ORDER BY C.EVS010_CREATE_S DESC")
    CommandStatus findCommandStatusByVinOrderByCreatedTimestampDesc(@Param("vin") String vin);
Vijai :

Since the query is SQL (and not JPQL) one needs to set nativeQuery = true in the annotation:

@Query(nativeQuery = true, value = "SELECT Top 1 * FROM NEVS010_VEH_ACTV_COMMAND C WHERE C.EVS014_VIN = :vin ORDER BY C.EVS010_CREATE_S DESC")
    CommandStatus findCommandStatusByVinOrderByCreatedTimestampDesc(@Param("vin") String vin);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=113806&siteId=1