简体   繁体   English

使用 ABAP 中的一个或多个 IF 语句进行 SELECT 查询

[英]SELECT query with one or multiple IF statements in ABAP

I'm working on a web service.我正在开发一个网络服务。 I have a select statement like this (it has much more fields and 2 more joins than this but to keep it simple):我有一个这样的 select 语句(它有更多的字段和 2 个以上的连接,但为了简单起见):

SELECT acdoca~augbl,
       open_amount,    "I need to add value to this field based on the IF statement below
       acdoca~koart
FROM acdoca
      LEFT JOIN bseg ON acdoca~belnr EQ bseg~belnr
                      AND acdoca~gjahr EQ bseg~gjahr
                      AND acdoca~buzei EQ bseg~buzei
                      AND acdoca~rbukrs EQ bseg~bukrs
INTO table @it_acdoca.

I need to add this IF statement:我需要添加这个 IF 语句:

IF bseg-KOART = 'D' or bseg-KOART = 'K' 
    IF acdoca-AUGBL is INITIAL THEN open_amount = DMBTR
        ELSE open_amount = 0
    ENDIF.
ELSE open_amount = 0
ENDIF.

I need an answer that won't trash the performance of the service.我需要一个不会破坏服务性能的答案。 Since ACDOCA and BSEG are some of the biggest database tables in SAP.由于 ACDOCA 和 BSEG 是 SAP 中一些最大的数据库表。 I tried using LOOP after SELECT and then adding this IF inside that LOOP but that destroyed the performance totally.我尝试在 SELECT 之后使用 LOOP,然后在该 LOOP 中添加这个 IF 但这完全破坏了性能。

This Is What IT_ACDOCA table should look like after这是 IT_ACDOCA 表之后的样子

Hoping for a quick reply.希望得到快速答复。 Best regards.此致。

DATA: BEGIN OF it_acdoca  OCCURS 0,
        augbl       TYPE acdoca-augbl,
        open_amount TYPE bseg-dmbtr,
        koart       TYPE acdoca-koart.
DATA: END OF it_acdoca.

SELECT acdoca~augbl,
       CASE WHEN bseg~koart NE 'D' AND bseg~koart NE 'K' THEN 0
            WHEN acdoca~augbl IS INITIAL THEN dmbtr
            ELSE 0
       END AS open_amount,
       acdoca~koart
FROM acdoca
      LEFT JOIN bseg ON acdoca~belnr EQ bseg~belnr
                      AND acdoca~gjahr EQ bseg~gjahr
                      AND acdoca~buzei EQ bseg~buzei
                      AND acdoca~rbukrs EQ bseg~bukrs
INTO TABLE @it_acdoca.

If your version not supported CASE you can prepare different queries for statement and merge responses.如果您的版本不支持CASE,您可以为语句和合并响应准备不同的查询。 Also you can run these queries in parallel.您也可以并行运行这些查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM