在以下查询中添加“ NULLS LAST”时,出现以下异常:
引起原因:com.microsoft.sqlserver.jdbc.SQLServerException:未为参数编号11设置值。
如果我删除NULLS LAST一切正常
如果我仅通过一个特定的列删除CASE WHEN代码并对其进行排序,则它可以与NULLS LAST一起使用,但是我需要CASE WHEN上的所有列。
@Query("SELECT c FROM ClassSpecificationTableEntity c"
+ " LEFT JOIN c.owner o "
+ " LEFT JOIN c.domain d "
+ "WHERE ISNULL(c.classStructure.classification.description, '') LIKE :SEARCH "
+ " OR ISNULL(c.classStructure.description, '') LIKE :SEARCH "
+ " OR ISNULL(d.description, '') LIKE :SEARCH "
+ " OR ISNULL(o.description, '') LIKE :SEARCH "
+ " OR ISNULL(c.measurementUnit, '') LIKE :SEARCH "
+ " OR ISNULL(c.defaultValue, '') LIKE :SEARCH "
+ " OR ISNULL(c.dataType, '') LIKE :SEARCH "
+ " OR ISNULL(c.tooltip, '') LIKE :SEARCH "
+ " ORDER BY "
+ " CASE :ORDERBY" +
" WHEN 0 THEN c.classStructure.classification.description " +
" WHEN 1 THEN c.assetAttribId " +
" WHEN 2 THEN c.dataType " +
" WHEN 3 THEN c.measurementUnit " +
" WHEN 4 THEN c.domain.description " +
" WHEN 5 THEN c.owner.description " +
" WHEN 6 THEN c.defaultValue " +
" WHEN 7 THEN c.tooltip " +
" END DESC NULLS LAST"
)
Page<ClassSpecificationTableEntity> findByLikeSearchDESC(@Param(value="SEARCH") final String searchCrit, final Pageable pageable, @Param(value="ORDERBY") final String orderBy);
最佳答案
T-SQL没有NULLS LAST
语法。
如果要最后排序NULL
值,通常的方法是使用CASE
表达式或IIF
:
--CASE Expression
CASE WHEN {expression} IS NULL THEN 1 ELSE 0 END
--IIF funciton (which is actually a shorthand CASE expression)
IIF({Expression} IS NULL, 1, 0)
如果您有一个复杂的表达式,想要最后对
NULL
值进行排序,然后对表达式进行排序,并且该表达式未出现在SELECT
中(因此不能被其别名引用),则可以移动将表达式添加到FROM
,以避免多次键入表达式:FROM ...
JOIN ...
LEFT JOIN ...
...
CROSS APPLY (VALUES({Expression}))V(Alias)
WHERE ...
ORDER BY IIF(V.Alias IS NULL,1,0),
V.Alias
关于java - 最后添加NULLS时的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58555658/