如何根据method参数做出案例陈述?

时间:2019-01-30 07:58:31

标签: java jpql

我想像此查询一样做出case语句:

@Query(nativeQuery = true, value = "select CASE " + 
            " WHEN TYP_0=1 THEN ACC_0" + 
            " WHEN TYP_0=2 THEN ACC_3" + 
            " WHEN TYP_0=3 THEN ACC_2" + 
            " END  FROM  tomx3v6.TOMCTB.GACCCODE  WHERE ACCCOD_0 IN (SELECT [ACCCOD_0] FROM [tomx3v6].[TOMCTB].[ITMMASTER] WHERE YITMOXA_0=?1) AND COA_0='PCG'")
    public String getPCGvalue(String linpurtyp, String code);

但是,我不想让案例基于数据库列TYP_0,而是想使case语句基于方法参数linpurtyp。该怎么做?

1 个答案:

答案 0 :(得分:1)

尝试一下:

@Query(nativeQuery = true, value = "select CASE " + 
            " WHEN ?1=1 THEN ACC_0" + 
            " WHEN ?1=2 THEN ACC_3" + 
            " WHEN ?1=3 THEN ACC_2" + 
            " END  FROM  tomx3v6.TOMCTB.GACCCODE  WHERE ACCCOD_0 IN (SELECT [ACCCOD_0] FROM [tomx3v6].[TOMCTB].[ITMMASTER] WHERE YITMOXA_0=?1) AND COA_0='PCG'")
    public String getPCGvalue(String linpurtyp, String code);

或者,使用命名参数

@Query(nativeQuery = true, value = "select CASE " + 
            " WHEN :linpurtyp=1 THEN ACC_0" + 
            " WHEN :linpurtyp=2 THEN ACC_3" + 
            " WHEN :linpurtyp=3 THEN ACC_2" + 
            " END  FROM  tomx3v6.TOMCTB.GACCCODE  WHERE ACCCOD_0 IN (SELECT [ACCCOD_0] FROM [tomx3v6].[TOMCTB].[ITMMASTER] WHERE YITMOXA_0=:linpurtyp) AND COA_0='PCG'")
    public String getPCGvalue(@Param("linpurtyp") String linpurtyp, @Param("code") String code);