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: 

SELECT... INTO CORRESPONDING FIELDS OF TABLE

Former Member
0 Kudos

Let's say I did one SELECT using INTO CORRESPONDING FIELDS OF TABLE itab.

Let's consider the table I selected from had the fields A, B, and C.

Now let's assume I do another select, now using FOR ALL ENTRIES, because I need the previous populated internal table - but now I'm selecting from a table that contains fields B, C and D.

Assuming my destination table itab is made of a structure of A, B, C and D, is it expected at the end that I whatever values I might have had for field A I lose them, because the 2nd select was made against a table that doesn't have it ?

Because that's what is happening, and I would like to keep the values of A the 1st query brought me. But when I execute the 2nd query, it brings new rows - ok - but all the values I had for field A, which doesn't exist in this table in the FROM clause in the 2nd select, get erased.

Is there a way to prevent this from happening ?

In terms of code:

TYPES:  BEGIN OF ty_zcar,
          vbeln TYPE likp-vbeln,
          exidv TYPE vekp-exidv,
          venum TYPE vekp-venum,
        END OF ty_zcar,

        tt_zcar TYPE STANDARD TABLE OF ty_zcar.

DATA:   gt_zcar TYPE tt_zcar.

    SELECT * FROM likp
    INTO CORRESPONDING FIELDS OF TABLE lt_zcar
    WHERE likp~vbeln = p_vbeln.

  CHECK lt_zcar IS NOT INITIAL. "so the select below doesnt do a cross join with table lips

  SELECT * FROM lips
    INNER JOIN vepo
      ON  vepo~vbeln = lips~vbeln AND
          vepo~posnr = lips~posnr
    INTO CORRESPONDING FIELDS OF TABLE gt_zcar
    FOR ALL ENTRIES IN lt_zcar
    WHERE lips~vbeln = lt_zcar-vbeln.

  lt_zcar = gt_zcar.

  SELECT exidv FROM vekp
    INTO CORRESPONDING FIELDS OF TABLE gt_zcar
    FOR ALL ENTRIES IN lt_zcar
    WHERE vekp~venum = lt_zcar-venum.

What happens is that VEKP doesn't have the field VBELN, while all the others above the last select do, but I'm losing the values I got at so far at the end.

Thanks

Avraham

1 ACCEPTED SOLUTION

ThomasZloch
Active Contributor
0 Kudos

Why don't you fill GT_ZCAR in one go using a JOIN select involving all four tables? They seem to have clear relationships, and you are selecting for one VBELN only. This might get rid of the problem.

Thomas

3 REPLIES 3

ThomasZloch
Active Contributor
0 Kudos

Why don't you fill GT_ZCAR in one go using a JOIN select involving all four tables? They seem to have clear relationships, and you are selecting for one VBELN only. This might get rid of the problem.

Thomas

Former Member
0 Kudos

Hello Avraham,

Your programming approach is not correct.

If you want to update only a single field in an internal table, you need to first populate that field and the linking field into a new internal table

and then ( logic is as follows)

LOOP at first internal table.

Read the second internal table using the linking field.

update firsdt internal table with field value from second internal table

ENDLOOP.

-


See the corrected code below.

TYPES: BEGIN OF ty_zcar,

vbeln TYPE likp-vbeln,

exidv TYPE vekp-exidv,

venum TYPE vekp-venum,

END OF ty_zcar,

tt_zcar TYPE STANDARD TABLE OF ty_zcar.

DATA: gt_zcar TYPE tt_zcar WITH HEADER LINE,

lt_zcar TYPE tt_zcar WITH HEADER LINE.

types : begin of ty_vekp,

venum like vekp-venum,

exidv like vekp-exidv,

end of ty_vekp.

data : i_vekp type ty_vekp OCCURS 0 WITH HEADER LINE.

SELECTION-SCREEN begin of block b1.

parameters : p_vbeln like vbak-vbeln.

SELECTION-SCREEN end of block b1.

SELECT * FROM likp

INTO CORRESPONDING FIELDS OF TABLE lt_zcar

WHERE likp~vbeln = p_vbeln.

CHECK lt_zcar[] IS NOT INITIAL. "so the select below doesnt do a cross join with table lips

SELECT * FROM lips

INNER JOIN vepo

ON vepovbeln = lipsvbeln AND

vepoposnr = lipsposnr

INTO CORRESPONDING FIELDS OF TABLE gt_zcar

FOR ALL ENTRIES IN lt_zcar

WHERE lips~vbeln = lt_zcar-vbeln.

lt_zcar[] = gt_zcar[].

*----


  • CORRECTED CODE

*----


SELECT exidv FROM vekp

INTO CORRESPONDING FIELDS OF TABLE i_vekp

FOR ALL ENTRIES IN lt_zcar

WHERE vekp~venum = lt_zcar-venum.

refresh lt_zcar.

loop at gt_zcar.

read table i_vekp with key venum = gt_zcar-venum.

if sy-subrc = 0.

gt_zcar-exidv = i_vekp-exidv.

endif.

append gt_zcar to lt_zcar.

endloop.

refresh gt_zcar.

gt_zcar[] = lt_zcar[].

Regards,

Jyothi

Hello Avraham,

Instead of using INTO CORRESPONDING FIELDS in the second select, use the statement APPENDING CORRESPONDING FIELDS to solve your issue.

Regards

Farzan