Application Development and Automation 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: 
Read only

inserting to itabs

Former Member
0 Likes
711

Do MOVE:... to itab-... statements insert new records to itabs within a loop? Or will it continually replace the first record in the internal table?

Ex.

Loop at TableA.
  Move: TABLEA-NAME TO vName.
  SELECT NAME INTO tempname FROM TableB WHERE NAME=vName.
  ENDSELECT.
  IF SY-SUBRC <> 0.
     MOVE: tempname  INTO ITAB_nameholder-Name.
  ENDIF.
ENDLOOP.

The reason why I ask is because later down in my program I loop at my internal table, but it does not recognize it as a table. (Even though I declared it earlier.) So I think it may be that my inserts to the itab are not working.

N L

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
679

If your LOOP AT is syntactically correct, then you are

using tables with header lines.

I added a couple of lines to your code:

Loop at TableA.
  Move: TABLEA-NAME TO vName.
  SELECT NAME INTO tempname FROM TableB WHERE NAME=vName.
  ENDSELECT.
  IF SY-SUBRC <> 0.
     <b>CLEAR ITAB_NAMEHOLDER.</b>
     MOVE: tempname  INTO ITAB_nameholder-Name.
     <b>APPEND ITAB_NAMEHOLDER.</b>
  ENDIF.
ENDLOOP.

6 REPLIES 6
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
679

If your internal table has a header line, then moving values to it would only move it in the header line. Use APPEND ITAB statement after fill the header line.

ex..

Data: imara type table of mara with header line.
Data: itab type table of mara with header line.

loop at imara.
move-corresponding imara to itab.
append itab.
endloop.

if your itab is defined like...

data: begin of itab <b>occurs 0 </b>,
      field(1) type c,
      end of itab.

then by default it has a header line.

Regards,

Rich Heilman

Message was edited by: Rich Heilman

Read only

0 Likes
679

my itab is defined like

data: begin of itab occurs 0 , 
            field(1) type c,      
            end of itab.

and when I try to append the itab, it says my syntax should be like:


APPEND fld TO itab

I'm reading up on APPEND in F1, but is APPEND similar to INSERT in SQL?

like, instead of doing

MOVE: VAL to ITAB-val.

should I do:

APPEND VAL TO ITAB-VAL.

?? (I require several inserts to the different fields of the itab.)

Natasha

Read only

0 Likes
679

if my itab contains fields name, value, catalog.

Can I do an append like:

APPEND NAME, VALUE, CATALOG TO ITAB.

? If name, value, and catalog correspond are variables and correspond to the field names of the itab.

I saw something similar in F1, but it looked like it only inserted values to an itab that only had one field- basically a list. I'm going to test this now.

N L

Read only

0 Likes
679

You should be filling all your fields of the table structure then doing an append.

ex..



data: begin of itab occurs 0,
      field1 type c,
      field2 type c,
      field3 type c,
      end of itab.

itab-field1 = 'A'.
itab-field2 = 'B'.
itab-field3 = 'C'.
append itab.

If your internal table has no header line, then you need a work area to hold you data.



types: begin of ttab,
      field1 type c,
      field2 type c,
      field3 type c,
      end of ttab.

data: itab type table of ttab.
data: xtab type ttab.

xtab-field1 = 'A'.
xtab-field2 = 'B'.
xtab-field3 = 'C'.
append xtab to itab.

Regards,

Rich Heilman

Read only

0 Likes
679

the reason why my itabs were not getting populated was because I was looping at a database table.

Thanks for your help gentlemen.

N L

Read only

Former Member
0 Likes
680

If your LOOP AT is syntactically correct, then you are

using tables with header lines.

I added a couple of lines to your code:

Loop at TableA.
  Move: TABLEA-NAME TO vName.
  SELECT NAME INTO tempname FROM TableB WHERE NAME=vName.
  ENDSELECT.
  IF SY-SUBRC <> 0.
     <b>CLEAR ITAB_NAMEHOLDER.</b>
     MOVE: tempname  INTO ITAB_nameholder-Name.
     <b>APPEND ITAB_NAMEHOLDER.</b>
  ENDIF.
ENDLOOP.