案例声明不适用于PSQUERY Where子句

时间:2013-11-18 13:18:31

标签: sql oracle case decode

我有一个字段帐单类型ID,它假设有x,y和z值。帐单类型ID是一个提示,条件是运行psquery时,如果帐单类型ID值为空,则应返回帐单类型为id x,y和z的所有行。

以下是Statement和Decode函数的写法:

(CASE 
WHEN A.BILL_TYPE_ID = ' ' THEN A.BILL_TYPE_ID LIKE ' %' 
ELSE A.BILL_TYPE_ID = :3 
END ))
编辑(来自评论):

SELECT 
  C.NAME1, 
  TO_CHAR(A.FROM_DT,'YYYY-MM-DD'), 
  B.LINE_SEQ_NUM,  
  B.IDENTIFIER,  
  B.DESCR,  
  B.GROSS_EXTENDED_AMT,  
  A.INVOICE_AMOUNT, 
  C.SETID, 
  C.CUST_ID 
FROM PS_BI_HDR A, PS_BI_LINE B, PS_CUSTOMER C 
WHERE 
( 
  A.BUSINESS_UNIT = B.BUSINESS_UNIT 
  AND A.INVOICE = B.INVOICE 
  AND A.BILL_TO_CUST_ID = C.CUST_ID 
  AND A.BUSINESS_UNIT = :1 
  AND A.BILL_STATUS = :2 
  AND 
  ( 
    A.BILL_TYPE_ID = 
    (
      CASE 
        WHEN A.BILL_TYPE_ID = ' ' THEN A.BILL_TYPE_ID LIKE ' %' 
        ELSE A.BILL_TYPE_ID = :3 
      END 
    )
  ) 
) ORDER BY 1, 3

使用上面的case语句我收到错误:缺少关键字,我无法调试缺少哪个关键字。请指教。

Decode(:3,' ','%',:3)

使用Decode函数我没有收到错误但是当bill type id为空时它不会返回任何行。请帮忙。

2 个答案:

答案 0 :(得分:1)

如果输入列表为空,您只需将A.BILL_TYPE_ID与自身进行比较:

SELECT 
  C.NAME1, 
  TO_CHAR(A.FROM_DT,'YYYY-MM-DD'), 
  B.LINE_SEQ_NUM,  
  B.IDENTIFIER,  
  B.DESCR,  
  B.GROSS_EXTENDED_AMT,  
  A.INVOICE_AMOUNT, 
  C.SETID, 
  C.CUST_ID 
FROM PS_BI_HDR A, PS_BI_LINE B, PS_CUSTOMER C 
WHERE 
( 
  A.BUSINESS_UNIT = B.BUSINESS_UNIT 
  AND A.INVOICE = B.INVOICE 
  AND A.BILL_TO_CUST_ID = C.CUST_ID 
  AND A.BUSINESS_UNIT = :1 
  AND A.BILL_STATUS = :2 
  AND 
  ( 
    A.BILL_TYPE_ID = 
    (
      CASE 
        WHEN :3 = ' ' THEN A.BILL_TYPE_ID
        ELSE :3 
      END 
    )
  ) 
) ORDER BY 1, 3

答案 1 :(得分:0)

在where子句中你可以使用这样的例子:

WHERE
A.BILL_TYPE_ID = (CASE WHEN A.BILL_TYPE_ID = ' ' THEN ' %' ELSE  ' 3' END)