简体   繁体   中英

Concatenating strings in SAP Query

I'm creating a BAPI for SAP R/3. The equivalent in MSSQL of what I'm trying to write is this:

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 )
;

But apparently Open Sql doesn't allow operations in queries. So for what I researched, the table I want to "join" must be retrieved to an in memory table, create a new column in a loop doing the operation and the compare with the #tab table. But I'm struggling with the syntax. What I have so far is something like this:

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.

But I'm getting the following errors.

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".

There's clearly an open statement. But I'm not really familiar with the language and don't know where the period is required or if I misplaced any closing statement. Other approach I saw online was with a construct 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.

But this generate a new batch of errors:

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.

I suspect the solutions and examples I found online skip some part where they define some of the structures they use. But I don't really know how to do it. Can someone help?

EDIT: The final code looks like that:

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.

manoftheyear.

Let me know if this works for you:

*** 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.

Cheers.

You can use CONCAT directly on QUERY. Example:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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