SELECT 空条件的默认结果?

SELECT with default result for empty condition?

我有一个包含以下字段和值的数据库 table:

Bukrs | Werks | Btrtl | Persg | Persk | value
1000   1000     1001     1      20      1,20
1000                                    1,10

我正在使用以下键构建查询:

SELECT
FROM dbtab
INTO lv_value
WHERE bukrs = bukrs
    AND (
        werks = werks
        AND btrtl = btrtl
        AND persg = persg
        AND persk = persk
        )

所以对于精确条件匹配的查询,value必须是1,20,这是正确的。

但对于另一个没有完全匹配的条件,例如bukrs = 1000 werks = 1002 btrtl = 1003 persg = 2 persk = 27,必须是1,10。

因此,所有不适合全键的内容都应回退到值为 1,10 的默认行。例如:

安德鲁有以下参数

Bukrs = 1000
Werks = 1000 
Btrtl = 1001
Persg = 1
Persk = 20

Peter 有以下参数

Bukrs = 1000
Werks = 1004
Btrtl = 1002
Persg = 1
Persk = 27

对于 Andrew,应选择 1,20,对于 Peter,应选择 1,10。

所以我认为您是在根据参数值进行匹配。这是我在黑暗中拍摄的照片,因为我对 ABAP 一无所知。

SELECT
FROM dbtab
INTO lv_value
WHERE bukrs = bukrs
ORDER BY DECODE(
    (       werks = @werks
        AND btrtl = @btrtl
        AND persg = @persg
        AND persk = @persk
    ), 2,
    (werks IS NULL), 1
    0
)
UP TO 1 ROWS;

试试这个方法:

DATA: Andrew TYPE dbtab.

Andrew-bukrs = '1000'.
Andrew-werks = '1000'.
Andrew-btrtl = '1001'.
Andrew-persg = '1'.
Andrew-persk = '20'.

SELECT SINGLE value INTO @DATA(Andy) 
  FROM dbtab WHERE werks = ANY ( SELECT CASE WHEN bukrs = @Andrew-bukrs 
                                              AND werks = @Andrew-werks 
                                              AND btrtl = @Andrew-btrtl
                                              AND persg = @Andrew-persg 
                                              AND persk = @Andrew-persk
                                        THEN werks 
                                        ELSE '9999' 
                                 END AS werks FROM dbtab 
                               ).

WRITE / Andy-value.

这个片段给你 1.20,而对只有 BUKRS 的人的相同查询给你 1.10。

DATA: Peter TYPE dbtab.

Peter-bukrs = '1000'.

SELECT ....

说明:这里我们通过子查询为ABAPSQL模拟XOR operator。我们的子查询总是 returns two 值:只有一个来自 dbtab table 的真实行,另一个总是一个虚拟的 9999。这样外部查询将始终 return 工厂的单个值,无论是真实的还是虚拟的。

此解决方案的先决条件:9999(子查询工厂)必须始终不存在于 dbtab

我这样解决了我的问题:

  SELECT * FROM  dbtab
  INTO CORRESPONDING FIELDS OF TABLE LT_TAB
  WHERE  APPLICATION  = LV_APPL
  AND ( BUKRS = E_PERNR-BUKRS OR BUKRS IS NULL OR BUKRS = SPACE )
  AND ( WERKS = E_PERNR-WERKS OR WERKS IS NULL OR WERKS = SPACE )
  AND ( BTRTL = E_PERNR-BTRTL OR BTRTL IS NULL OR BTRTL = SPACE )
  AND ( PERSG = E_PERNR-PERSG OR PERSG IS NULL OR PERSG = SPACE )
  AND ( PERSK = E_PERNR-PERSK OR PERSK IS NULL OR PERSK = SPACE )
  AND    ENDDA       GE E_BEGDA
  AND    BEGDA       LE E_BEGDA
ORDER BY BUKRS DESCENDING WERKS DESCENDING BTRTL DESCENDING PERSG DESCENDING PERSK DESCENDING.

 READ TABLE LT_TAB INTO LS_TAB INDEX 1.
 LV_KWERT = LS_TAB-KONST.