在 SAP 查询中连接字符串

Concatenating strings in SAP Query

我正在为 SAP 创建一个 BAPI R/3。我正在尝试编写的 MSSQL 中的等价物是:

select
    bkpf.BELNR,
    bkpf.BUKRS,
    bkpf.GJAHR,
    bkpf.AWKEY
into
    #tab
from
    bkpf
where
    exists ( select 1 from #n_tab n where CONCAT(n.BELNR, n.GJAHR) = bkpf.AWKEY )
;

但显然 Open Sql 不允许在查询中进行操作。因此,对于我的研究,我想要 "join" 的 table 必须检索到内存中的 table,在循环中创建一个新列来执行操作并与 #tab 进行比较table。但我在语法上苦苦挣扎。到目前为止,我所拥有的是这样的:

FUNCTION ZBAPI_TEST.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"----------------------------------------------------------------------

select 
    bkpf~BELNR
    bkpf~BUKRS
    bkpf~GJAHR
    bkpf~AWKEY
into ITAB_BKPF
from bkpf.

loop at ITAB_BKPF.
ITAB_BKPF-chkey = CONCATENATE BELNR GJAHR.
modify ITAB_BKPF.
endloop.

ENDFUNCTION.

但我收到以下错误。

Field "ITAB_BKPF" is unknown. It is neither in one of the specified tables nor defined by a "DATA" statement.

Field "ITAB_BKPF-GJAHR" is unknown. It is neither in one of the specified tables nor defined by a "DATA" statement.

Incorrect nesting: Before the statement "ENDFUNCTION", the control structure introduced by "SELECT" must be concluded with "ENDSELECT".

Incorrect nesting: Before the statement "+END-OF-INCLUDE", the control structure introduced by "FUNCTION" must be concluded with "ENDFUNCTION".

显然有公开声明。但我不太熟悉这种语言,也不知道在哪里需要句号,或者我是否放错了任何结束语。我在网上看到的其他方法是使用构造 SELECT..ENDSELECT:

FUNCTION ZBAPI_TEST.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"----------------------------------------------------------------------

select
    bkpf~belnr
    bkpf~bukrs
    bkpf~gjahr
    bkpf~awkey
into corresponding fields
    wa_bkpf
from bkpf.

wa_bkpf-chkey = concatenate belnr gjahr.
append wa_bkpf to itab_bkpf.
endselect.

ENDFUNCTION.

但这会产生新的一批错误:

Field "CORRESPONDING" is unknown. It is neither in one of the specified tables nor defined by a "DATA" statement. Field "WA_BKPF-CHKEY" is unknown. It is neither in one of the specified tables nor defined by a "DATA" statement. Field "WA_BKPF" is unknown. It is neither in one of the specified tables nor defined by a "DATA" statement.

我怀疑我在网上找到的解决方案和示例跳过了一些他们定义他们使用的结构的部分。但我真的不知道该怎么做。有人可以帮忙吗?

编辑: 最终代码如下所示:

types: begin of t_bkpf,
            belnr type belnr_d,
            bukrs type bukrs,
            gjahr type gjahr,
            awkey type awkey,
            chkey type string.
types: end of t_bkpf.
data: itab_bkpf type standard table of t_bkpf.
field-symbols: <wa> type t_bkpf.

    select
        BELNR
        BUKRS
        GJAHR
        AWKEY
    into corresponding fields of table ITAB_BKPF
    from bkpf.

    loop at ITAB_BKPF assigning <wa>.
      CONCATENATE <wa>-BELNR <wa>-GJAHR into <wa>-chkey.
    endloop.

年度大奖。

让我知道这是否适合你:

*** Definition of custom type.
TYPES: BEGIN OF ty_bkpf,
        belnr TYPE bukrs,
        bukrs TYPE belnr_d,
        gjahr TYPE gjahr,
        awkey TYPE awkey,
        chkey TYPE string,    " Replace with the type you need to use.
       END OF ty_bkpf.

*** Definition of internal table of custom type.
DATA lt_bkpf TYPE STANDARD TABLE OF ty_bkpf.

*** Definition of field symbol (pointer) of custom type.
FIELD-SYMBOLS <lfs_bkpf> TYPE ty_bkpf.


*** Extraction of data from BKPF database table.
SELECT belnr bukrs gjahr awkey
  FROM bkpf
  INTO CORRESPONDING FIELDS OF TABLE lt_bkpf.

*** Checks if extraction was succesful.
IF sy-subrc IS INITIAL.

  UNASSIGN <lfs_bkpf>.

*** Loop internal table...
  LOOP AT lt_bkpf ASSIGNING <lfs_bkpf>.

*** ...and create value for field CHKEY.
    CONCATENATE <lfs_bkpf>-belnr
                <lfs_bkpf>-gjahr
           INTO <lfs_bkpf>-chkey.  " By using a pointer, there's no need to use MODIFY sentence.

  ENDLOOP.

ENDIF.

干杯。

您可以直接对 QUERY 使用 CONCAT。示例:

SELECT SINGLE CONCAT( CONCAT( a~name1, a~name2 ), a~name3 )
        FROM ( ( adrc AS a
             INNER JOIN j_1bbranch AS j ON a~addrnumber = j~adrnr )
             INNER JOIN t001w AS t ON j~cgc_branch = t~j_1bbranch
      AND j~bukrs = @i_postab-bukrs
      AND t~werks = @e_postab-prctr+4(4) )
      INTO @e_postab-company_name.