I am having a query that uses a declare statement before executing the actual query. As I am using MSSQL server. So, my query is like the the below:
DECLARE @IdParam NVARCHAR(50) = (
SELECT TOP 1 PartyID
FROM PartyInfo
WHERE PartyName = ?1
ORDER BY IsPrimary DESC);
SELECT *
FROM TutorialInfo
WHERE UserId = @IdParam
And my repository has the following method that call native NamedQuery
@Query(nativeQuery = true)
List<UserConvictedCases> getUserConvictedCasesByQid(String qid, Pageable pageable);
My issue that when I execute that method, I am getting the below exception because the pageable is being inserted in the wrong place in the query.
failed: org.springframework.dao.InvalidDataAccessResourceUsageException: JDBC exception executing SQL [DECLARE @IdParam NVARCHAR(50) = (
SELECT TOP 1 PartyID
FROM PartyInfo
WHERE PartyName = ?1
ORDER BY IsPrimary DESC) order by @@version offset 0 rows fetch first ? rows only;
I have a workaround of course for this, that I will use the declared variable in each place I need it. But my question is about is there anyway that I can get this to work and put the pageable parameter in the right place in the query?
Thank you in advance