如何加入表 KNBK 和 TIBAN?
How to join the tables KNBK and TIBAN?
我试图读取 table TIBAN
仅在 KNBK
table 的字段 BANKN
包含字符串“IBAN”的情况下”。我遇到的问题是阅读 table TIBAN
。对于 KNBK
table 我使用关键字 kunnr
搜索 table,但 table TIBAN
不包含此字段。代码如下:
LOOP AT lt_data_bsec ASSIGNING <ls_data_bsec>.
READ TABLE lt_data_knbk ASSIGNING FIELD-SYMBOL(<ls_data_knbk>)
WITH KEY kunnr = <ls_data_bsec>-kunnr BINARY SEARCH .
IF <ls_data_knbk>-bankn_kn CS '<IBAN>'.
>>>>> READ TABLE lt_data_tiban ASSIGNING FIELD-SYMBOL(<ls_data_tiban>).
IF <ls_data_bsec> IS ASSIGNED
AND <ls_data_tiban> IS ASSIGNED
AND ( <ls_data_bsec>-banks_bs NE <ls_data_tiban>-banks_kn
OR <ls_data_bsec>-bankl_bs NE <ls_data_tiban>-bankl_kn
OR <ls_data_bsec>-bankn_bs NE <ls_data_tiban>-bankn_kn ).
ENDIF.
lt_data_knbk
table 的声明是:
SELECT kunnr,
banks AS banks_kn,
bankl AS bankl_kn,
bankn AS bankn_kn
FROM knbk
INTO TABLE @DATA(lt_data_knbk)
FOR ALL ENTRIES IN @lt_data_bsec
WHERE kunnr = @lt_data_bsec-kunnr
ORDER BY PRIMARY KEY.
而 lt_data_tiban
的声明是:
SELECT banks AS banks_tb,
bankl AS bankl_tb,
bankn AS bankn_tb
FROM tiban
INTO TABLE @DATA(lt_data_tiban).
lt_data_bsec
的声明也是 gty_out
:
类型
BEGIN OF gty_out,
bukrs TYPE bukrs,
belnr TYPE belnr_d,
buzei TYPE buzei,
budat TYPE budat,
kunnr TYPE kunnr,
banks_kn TYPE banks,
bankl_kn TYPE bankk,
bankn_kn TYPE bankn,
banks_bs TYPE banks,
bankl_bs TYPE bankk,
bankn_bs TYPE bankn,
banks_tb TYPE banks,
bankl_tb TYPE bankk,
bankn_tb TYPE bankn,
END OF gty_out,
我在代码中标记的行中有一个错误,因为它需要一个关键字段来读取 table。
有人知道我需要什么类型的字段来搜索 TIBAN
table,就像我搜索 KNBK
table 一样吗?
我建议您使用 TIBAN LEFT OUTER JOIN
来获取您的银行数据。
SELECT knbk~kunnr AS kunnr,
knbk~banks AS banks_kn,
knbk~bankl AS bankl_kn,
knbk~bankn AS bankn_kn,
knbk~bkont AS bkont_kn,
tiban~iban AS iban
FROM knbk
LEFT OUTER JOIN tiban ON
tiban~banks = knbk~banks AND
tiban~bankl = knbk~bankl AND
tiban~bankn = knbk~bankn AND
tiban~bkont = knbk~bkont
INTO TABLE @DATA(lt_data_knbk)
FOR ALL ENTRIES IN @lt_data_bsec
WHERE knbk~kunnr = @lt_data_bsec-kunnr
ORDER BY knbk~banks knbk~bankl knbk~bankn knbk~bkont.
结果将是一个 table,其中包含 KNBK
中的所有条目,加上一个字段 IBAN
,如果存在相应的行,则该字段将从 TIBAN
中填充并且是当不存在相应行时初始。
现在您只需确定您是否拥有这样的 IBAN:
LOOP AT lt_data_knbk ASSIGNING FIELD-SYMBOL(<ls_data_knbk>).
IF <ls_data_knbk>-iban IS INITIAL.
" classic bank number + account number account
ELSE.
" IBAN account
ENDIF.
ENDLOOP.
你的任务是 ABAP CDS associations aka Lazy Join 发挥作用的完美案例。
如果您使用的是最新版本的 ABAP 并且能够创建 CDS,则没有什么可以阻止您创建这个
@AbapCatalog.sqlViewName: ‘ZCUST_TIBAN_SQL’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: ‘TIBAN’
define view ZCUST_TIBAN as select from knbk as cus
association [0..1] to tiban as _an
on cus.kunnr = _an.kunnr
and cus.banks = _an.banks
and cus.bankl = _an.bankl
and cus.bankn = _an.bankn
and cus.bkont = _an.bkont
{
cus.kunnr,
cus.banks AS banks_kn,
cus.bankl AS bankl_kn,
cus.bankn AS bankn_kn,
cus.bkont AS bkont_kn,
_iban~
} WHERE cus.bankn like '%IBAN%'
然后您可以在所有进一步的要求中使用此关联:
SELECT banks_kn, bankl_kn, bankn_kn, bkont_kn
\_iban-iban AS IBAN,
\_iban-valid_from AS IBAN_validity
FROM zcust_tiban
WHERE kunnr IN @lr_customers
INTO TABLE @DATA(lt_data_knbk).
...
READ TABLE lt_data_knbk ASSIGNING FIELD-SYMBOL(<ls_data_knbk>)
WITH KEY kunnr = <ls_data_bsec>-kunnr BINARY SEARCH.
...
需要注意的要点:
- 像上面那样具有 0..1 基数的关联不会进行实际的 JOIN,直到像上面 SELECT 中那样请求来自关联 table
TIBAN
的字段。这比 Philipp 建议的 LEFT OUTER JOIN
方法更高效、更通用。
- 它是可重复使用的,可以作为源selects/associations进一步使用
我试图读取 table TIBAN
仅在 KNBK
table 的字段 BANKN
包含字符串“IBAN”的情况下”。我遇到的问题是阅读 table TIBAN
。对于 KNBK
table 我使用关键字 kunnr
搜索 table,但 table TIBAN
不包含此字段。代码如下:
LOOP AT lt_data_bsec ASSIGNING <ls_data_bsec>.
READ TABLE lt_data_knbk ASSIGNING FIELD-SYMBOL(<ls_data_knbk>)
WITH KEY kunnr = <ls_data_bsec>-kunnr BINARY SEARCH .
IF <ls_data_knbk>-bankn_kn CS '<IBAN>'.
>>>>> READ TABLE lt_data_tiban ASSIGNING FIELD-SYMBOL(<ls_data_tiban>).
IF <ls_data_bsec> IS ASSIGNED
AND <ls_data_tiban> IS ASSIGNED
AND ( <ls_data_bsec>-banks_bs NE <ls_data_tiban>-banks_kn
OR <ls_data_bsec>-bankl_bs NE <ls_data_tiban>-bankl_kn
OR <ls_data_bsec>-bankn_bs NE <ls_data_tiban>-bankn_kn ).
ENDIF.
lt_data_knbk
table 的声明是:
SELECT kunnr,
banks AS banks_kn,
bankl AS bankl_kn,
bankn AS bankn_kn
FROM knbk
INTO TABLE @DATA(lt_data_knbk)
FOR ALL ENTRIES IN @lt_data_bsec
WHERE kunnr = @lt_data_bsec-kunnr
ORDER BY PRIMARY KEY.
而 lt_data_tiban
的声明是:
SELECT banks AS banks_tb,
bankl AS bankl_tb,
bankn AS bankn_tb
FROM tiban
INTO TABLE @DATA(lt_data_tiban).
lt_data_bsec
的声明也是 gty_out
:
BEGIN OF gty_out,
bukrs TYPE bukrs,
belnr TYPE belnr_d,
buzei TYPE buzei,
budat TYPE budat,
kunnr TYPE kunnr,
banks_kn TYPE banks,
bankl_kn TYPE bankk,
bankn_kn TYPE bankn,
banks_bs TYPE banks,
bankl_bs TYPE bankk,
bankn_bs TYPE bankn,
banks_tb TYPE banks,
bankl_tb TYPE bankk,
bankn_tb TYPE bankn,
END OF gty_out,
我在代码中标记的行中有一个错误,因为它需要一个关键字段来读取 table。
有人知道我需要什么类型的字段来搜索 TIBAN
table,就像我搜索 KNBK
table 一样吗?
我建议您使用 TIBAN LEFT OUTER JOIN
来获取您的银行数据。
SELECT knbk~kunnr AS kunnr,
knbk~banks AS banks_kn,
knbk~bankl AS bankl_kn,
knbk~bankn AS bankn_kn,
knbk~bkont AS bkont_kn,
tiban~iban AS iban
FROM knbk
LEFT OUTER JOIN tiban ON
tiban~banks = knbk~banks AND
tiban~bankl = knbk~bankl AND
tiban~bankn = knbk~bankn AND
tiban~bkont = knbk~bkont
INTO TABLE @DATA(lt_data_knbk)
FOR ALL ENTRIES IN @lt_data_bsec
WHERE knbk~kunnr = @lt_data_bsec-kunnr
ORDER BY knbk~banks knbk~bankl knbk~bankn knbk~bkont.
结果将是一个 table,其中包含 KNBK
中的所有条目,加上一个字段 IBAN
,如果存在相应的行,则该字段将从 TIBAN
中填充并且是当不存在相应行时初始。
现在您只需确定您是否拥有这样的 IBAN:
LOOP AT lt_data_knbk ASSIGNING FIELD-SYMBOL(<ls_data_knbk>).
IF <ls_data_knbk>-iban IS INITIAL.
" classic bank number + account number account
ELSE.
" IBAN account
ENDIF.
ENDLOOP.
你的任务是 ABAP CDS associations aka Lazy Join 发挥作用的完美案例。
如果您使用的是最新版本的 ABAP 并且能够创建 CDS,则没有什么可以阻止您创建这个
@AbapCatalog.sqlViewName: ‘ZCUST_TIBAN_SQL’
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: ‘TIBAN’
define view ZCUST_TIBAN as select from knbk as cus
association [0..1] to tiban as _an
on cus.kunnr = _an.kunnr
and cus.banks = _an.banks
and cus.bankl = _an.bankl
and cus.bankn = _an.bankn
and cus.bkont = _an.bkont
{
cus.kunnr,
cus.banks AS banks_kn,
cus.bankl AS bankl_kn,
cus.bankn AS bankn_kn,
cus.bkont AS bkont_kn,
_iban~
} WHERE cus.bankn like '%IBAN%'
然后您可以在所有进一步的要求中使用此关联:
SELECT banks_kn, bankl_kn, bankn_kn, bkont_kn
\_iban-iban AS IBAN,
\_iban-valid_from AS IBAN_validity
FROM zcust_tiban
WHERE kunnr IN @lr_customers
INTO TABLE @DATA(lt_data_knbk).
...
READ TABLE lt_data_knbk ASSIGNING FIELD-SYMBOL(<ls_data_knbk>)
WITH KEY kunnr = <ls_data_bsec>-kunnr BINARY SEARCH.
...
需要注意的要点:
- 像上面那样具有 0..1 基数的关联不会进行实际的 JOIN,直到像上面 SELECT 中那样请求来自关联 table
TIBAN
的字段。这比 Philipp 建议的LEFT OUTER JOIN
方法更高效、更通用。 - 它是可重复使用的,可以作为源selects/associations进一步使用