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

Moving data from internal table to Data base table depends on Internal table order

3,204

Dears,

I have internal table with one field containing 5 values..
I need to move the internal table entries to DB table containing five fields.

Now i have to map first value of internal table to first field of DB table , second value of internal table to first field of DB table and so ..

It has to be achieved without using sy-tabix.
Can anyone suggest a solution for this?

Dears,

I have internal table with one field containing 5 values..
I need to move the internal table entries to DB table containing five fields.

Now i have to map first value of internal table to first field of DB table , second value of internal table to first field of DB table and so ..

It has to be achieved without using sy-tabix.
Can anyone suggest a solution for this?

10 REPLIES 10
Read only

Former Member
3,009

Hi,

First make an internal table of the DB with the 5 fields. Then use "move corresponding fields of internal table to the DB internal table". The fields of the internal table will be mapped to the DB fields respectively. Then you can modify the DB with the DB internal table.

Thanks,

M.Dey

Read only

3,009

Hello Dey,

Thanks for your response..

If internal table have five different fields ,we can use "MOVE CORRESPONDING" keyword..
But, for our case internal table have single field.
My pseudo code for this (with sy-tabix):

Loop at lt_itab into ls_wa.

if sy-tabix = '1'.

ls_dbtable_fieldname1 = ls_wa-table_line .

else if sy-tabix = '2'.

ls_dbtable-fieldname2 = ls_wa-table_line . and so

endloop.

It has to be achieved without using sy-tabix.

Could you please suggest a solution for the same?

Read only

Former Member
3,009

Ok, in this case, internal table with 1 field will have all the 5 values in a concatenated form. So you can split the values offset wise, accordingly, as per the data type length and assign them to your corresponding DB internal table fields. e.g. let's say you have the 5 field values of length 2 char each for the corresponding DB field. So,

Read table lt_itab into ls_wa index 1.

if sy-subrc = 0.

ls_dbtable_fieldname1 = ls_wa-table_line+0(2).

ls_dbtable_fieldname2 = ls_wa-table_line+2(2).... so on till 5 fields are complete.

endif.

Read only

Former Member
3,009

So, your internal table contains 5 rows and each one corresponds to a field in the database table.

The simplest method I can think of is this, if you are on release 7.40+:

table_structure-field1 = table_data[ 1 ].
table_structure-field2 = table_data[ 2 ].
...

If not, or you prefer something more generic (which doesn't require a modification if a new field is added to the table):

FIELD-SYMBOLS: <db_field> TYPE any.

LOOP AT data_fields INTO data_field.
  ASSIGN COMPONENT sy-tabix OF STRUCTURE table_structure TO <db_field>.
  <db_field> = data_field.
ENDLOOP.
Read only

Sandra_Rossi
Active Contributor
3,009

Is there any reason to do it "without using sy-tabix"?

Read only

Former Member
0 Likes
3,009

..........

Read only

0 Likes
3,009

OCCURS 0 has been obsolete for many years, please don't use it. And prefixes are not recommended anymore either.

The code is rather confusing and doesn't seem to answer OP's question (although the question itself is not very clear tbh). And what if a field value must include a space (e.g. 'JACK JOHNSON')?

Read only

Jelena_Perfiljeva
Active Contributor
3,009

Might want to clarify what exactly is the purpose of this and, just like Sandra, I'm not clear what exactly is the reason for not using sy-tabix.

This seems like some sort of student class assignment and I'm wondering if the teacher just didn't think this through or there is more to this that you haven't mentioned to us.

The "brute force" approach would seem like in an example posted as one of the comments below. Instead of sy-tabix you could use your own counter variable. But in real life, no one should write code like this. In addition to being amateurish, it also disregards any potential issues with the field type. E.g. what if you have a text value but DB field is numeric? Or today you have 5 fields but tomorrow there are 25. Honestly, the whole premise just doesn't seem serious. Please explain where this is coming from.

Read only

Former Member
0 Likes
3,009

data:

value1 type string value space,

value2 type string value space,

value3 type string value space,

value4 type string value space,

value5 type string value space,

ls_field1 type string value '1;2;3;4;5'. "for example

*

begin of data: ls_dbtable, "for example

field1 type string value space,

field2 type string value space,

field3 type string value space,

field4 type string value space,

field5 type string value space,

end of data.

*

begin of data: lt_dbtable,

field1 type string value space,

field2 type string value space,

field3 type string value space,

field4 type string value space,

field5 type string value space,

end of data.

*

data: lt_field1 type string.

*

*etc.

* do ...

* populate ls_field1 with 5 values separated by 1 semicolon each, or however you want;

append ls_field1 to lt_field1.

* enddo.

*

loop at lt_field1.

split lt_field1 at space into value1 value2 value3 value3 value4 value5.

* move value1, value2, value3, value4, value5 into DB table

ls_dbtable-field1 = value1.

ls_dbtable-field2 = value2.

ls_dbtable-field3 = value3.

ls_dbtable-field4 = value4.

ls_dbtable-field5 = value5.

append ls_dbtable to lt_dbtable.

clear: value1, value2, value3, value4, value5, ls_dbtable.

endloop.

* etc

Read only

Former Member
0 Likes
3,009

change from:

split lt_field1 at space into value1 value2 value3 value3 value4 value5.

to:

data: ld_semicolon char1 value ';'.

split lt_field1 at ld_semicolon into value1 value2 value3 value3 value4 value5.