最后添加NULLS时的问题

问题描述 投票:0回答:1

在以下查询上添加“ NULLS LAST”时,出现以下异常:

原因:com.microsoft.sqlserver.jdbc.SQLServerException:未为参数编号11设置值。

1)如果我删除NULLS LAST一切正常

2)如果我仅通过一个特定的列删除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);
java sql-server spring-boot jpql
1个回答
1
投票

没有NULLS LAST语法是T-SQL。

如果最后要订购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)
© www.soinside.com 2019 - 2024. All rights reserved.