As400 - 动态Sql where子句

时间:2014-02-06 15:53:06

标签: sql db2 db2-400

在哪里试图避免生成两个sql游标,因为我们只需要将WHERE子句更改为动态游标。

我们有一个游标执行此WHERE子句:

WHERE TRIM ( USUARIO ) LIKE @USUARIO ) 
AND CAST ( M15 . FECLLEGADA AS DATE ) BETWEEN @FINI AND @FFIN 
AND M15 . ESTINBOX IN ( 'A' , 'P' )
AND URGENTE LIKE ( COALESCE ( @URGENTE , '' ) CONCAT '%' ) 

和另一个带有此WHERE CLAUSE的游标:

WHERE 
    CAST ( M15 . FECLLEGADA AS DATE ) BETWEEN @FINI AND @FFIN 
    AND TRIM ( USUARIO ) LIKE @USUARIO 
    AND M15 . ESTINBOX LIKE ( COALESCE ( @ESTADO , '' ) CONCAT '%' )
    AND URGENTE LIKE ( COALESCE ( @URGENTE , '' ) CONCAT '%' ) 

有没有办法在一个语句中执行where where子句:¿

这是我尝试过的:

WHERE 
 CASE 
      WHEN @ESTADO='PR' then ESTINBOX IN ('A','P')
      ELSE M15.ESTINBOX(COALESCE (  @ESTADO , '' )    CONCAT '%' ) 
 END

存储过程未编译。如果这是可能的我怎么能附加到案件时 额外的“AND”条款

1 个答案:

答案 0 :(得分:3)

这样的事情,可能是吗?

... WHERE TRIM ( USUARIO ) LIKE @USUARIO 
      AND CAST ( M15.FECLLEGADA AS DATE ) BETWEEN @FINI AND @FFIN 
      AND URGENTE LIKE ( COALESCE ( @URGENTE , '' ) CONCAT '%' ) 
      AND ( 
         (@ESTADO = 'PR' AND M15.ESTINBOX IN ( 'A' , 'P' ))
         OR 
         (@ESTADO != 'PR' AND M15.ESTINBOX LIKE COALESCE ( @ESTADO , '' ) CONCAT '%' 
      )