带OR条件的SELECT错误结果

时间:2017-09-15 17:25:05

标签: select abap opensql

结果似乎该程序仅使用AUART =' YI01'获得所有销售订单(来自VBAK表),实际上我获得了1.699.698销售订单。所以它忽略了选择VTWEG = 'Z1'SPART = 'Z1'KUNNR = '0230001039'ERDAT = GT '01.09.2017'中的其他参数,我只能获得77个结果。

TYPES: BEGIN OF lw_odv_vbak,
        vbeln TYPE vbak-vbeln,
        vkorg TYPE vbak-vkorg,
        vtweg TYPE vbak-vtweg,
        spart TYPE vbak-spart,
        netwr TYPE vbak-netwr,
        kunnr TYPE vbak-kunnr,
        erdat type vbak-erdat,
      END OF lw_odv_vbak.


DATA: it_odv_vbak TYPE STANDARD TABLE OF lw_odv_vbak INITIAL SIZE 0.



SELECT vbeln
     FROM vbak
      INTO TABLE it_odv_vbak
      WHERE vkorg = 'Z326'
      AND vtweg = 'Z1'
      AND spart = 'Z1'
      AND kunnr = '0230001039'
      AND auart = 'YC01' OR
          auart = 'YI01'
        AND erdat GT '01.09.2017'.

非常感谢你的帮助

3 个答案:

答案 0 :(得分:3)

你可以做两件事: 1)在OR周围使用括号 2)将日期格式更改为“20170901”。在db中,日期存储为YYYYMMDD。

SELECT vbeln
 FROM vbak
  INTO TABLE it_odv_vbak
  WHERE vkorg = 'Z326'
  AND vtweg = 'Z1'
  AND spart = 'Z1'
  AND kunnr = '0230001039'
  AND ( auart = 'YC01' OR auart = 'YI01') 
  AND erdat GT '20170901'.

答案 1 :(得分:2)

AND的优先级高于OR,因此您可以有效地选择

( vkorg = 'Z326' AND vtweg = 'Z1' AND spart = 'Z1' AND kund = '0230001039' AND auart = 'YC01' )
  OR
( auart = 'YI01' AND erdet GT '01.09.2017' )

这肯定不是你打算做的。在OR子句周围使用括号。

答案 2 :(得分:1)

在OR

周围使用括号
SELECT vbeln
     FROM vbak
      INTO TABLE it_odv_vbak
      WHERE vkorg = 'Z326'
      AND vtweg = 'Z1'
      AND spart = 'Z1'
      AND kunnr = '0230001039'
      AND ( auart = 'YC01' OR
          auart = 'YI01' )
        AND erdat GT '01.09.2017'.