Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

SAP ABAP SQL I have 2 different select statement the 1st one is an input to the 2nd select.

calvinkarlo
Explorer
0 Kudos
432

Hi! SAP ABAP SQL I have 2 different select statement the 1st one is an input to the 2nd select statement.

TYPES : BEGIN OF Y_BWART,
BWART TYPE MSEG-BWART,
END OF Y_BWART.

DATA: TS_BWART TYPE STANDARD TABLE OF Y_BWART WITH HEADER LINE.

SELECT SUB2 "sub 2 is a char 2
FROM ZVXXPARAM_GLOBAL
INTO TABLE TS_BWART
WHERE MAIN EQ 'QR'.

SELECT MATNR
FROM MSEG
INTO TABLE @DATA(TS_MSEG)
WHERE BWART EQ @TS_BWART-Bwart.

But when I debug this one TS_MSEG is empty.

I checked mseg table with values from ts_bwart it has data.

please help me.

4 REPLIES 4

Sandra_Rossi
Active Contributor
0 Kudos
354

Please edit your question (Actions>Edit), select your code and press the button [CODE], which makes the code appear colored/indented, it'll be easier for people to look at it. Thanks!

ZHAMAMA
Explorer
0 Kudos
354

SELECT MATNR

FROM MSEG
INTO TABLE @DATA(TS_MSEG)
WHERE BWART EQ @TS_BWART-Bwart.

because TS_BWART is a Table an not a structure you cant use BWART EQ @TS_BWART-Bwart

you can use for all entries in TS_BWART

Sandra_Rossi
Active Contributor
0 Kudos
354

I think it's best if you continue the discussion with people in your previous question (Field "ts" is unkwown. It is neither one of the specified tables... | SAP Community), they would be happy to know the initial part which you had not shared with them:

TYPES : BEGIN OF Y_BWART,
          BWART TYPE MSEG-BWART,
        END OF Y_BWART.

DATA: TS_BWART TYPE STANDARD TABLE OF Y_BWART WITH HEADER LINE.

Sandra_Rossi
Active Contributor
354

I agree with the explanation given by sapcloud_education8.

Don't use Header Lines, it's obsolete and forbidden in modern programming, because it leads to errors such as the one you have encountered. Header lines are well known to be error prone.

I suggest that you use a join (here it's based on EXISTS, but you may also use the INNER JOIN form):

SELECT DISTINCT matnr
    FROM mseg
    INTO TABLE @DATA(ts_mseg)
    WHERE EXISTS ( SELECT * 
        FROM ZVXXPARAM_GLOBAL 
        WHERE sub2 = mseg~bwart
          AND main = 'QR' ).

NB: use DISTINCT to get unique values of MATNR.

Below is your code, with FOR ALL ENTRIES. NB: it's very important that you make sure TS_BWART is not initial otherwise you would extract all values of MATNR (it ignores the lines of WHERE which refer to TS_BWART):
TYPES : BEGIN OF Y_BWART,
          BWART TYPE MSEG-BWART,
        END OF Y_BWART.

DATA: TS_BWART TYPE STANDARD TABLE OF Y_BWART.

SELECT SUB2 "sub 2 is a char 2
    FROM ZVXXPARAM_GLOBAL
    INTO TABLE TS_BWART
    WHERE MAIN EQ 'QR'.
IF ts_bwart IS NOT INITIAL.
SELECT DISTINCT MATNR
    FROM MSEG
    INTO TABLE @DATA(TS_MSEG)
    FOR ALL ENTRIES IN @TS_BWART
    WHERE BWART EQ @TS_BWART-Bwart.
ENDIF.