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

Loop question

Former Member
0 Likes
591

hello,

I have a requirement to make records unique before updating a table. the scenario is as follows.

table - fields A to G ( A, B, C and D are primary )

The data provider changed so they cannot provide an entry for field D, meaning a combination of A B and C may not be unique.

Eg : A B C D

-


X01 032 202

X02 032 002

X02 032 002

X04 032 662

Records 2 and 3 are not unique, I need to make it unique by incrementing the value of D by 1 for each record before updating the table.

how do I loop within a combination of records in this case to update the field D ?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
561

Hi,

1 way is:

sort itab by a b c d

loop at itab into wa.

loop at itab where itab-a eq wa-a and itab-b eq wa-b and itab-c eq wa-c and itab-d eq wa-d.

add 1 to d.

modify itab.

exit.

endloop.

endloop.

Regards

Nicole

5 REPLIES 5
Read only

Former Member
0 Likes
562

Hi,

1 way is:

sort itab by a b c d

loop at itab into wa.

loop at itab where itab-a eq wa-a and itab-b eq wa-b and itab-c eq wa-c and itab-d eq wa-d.

add 1 to d.

modify itab.

exit.

endloop.

endloop.

Regards

Nicole

Read only

Former Member
0 Likes
561

Hello,

You can keep value of D as 1 for every entry. Also you need to check that a particular combination is exsisting already in the table. if yes then increment value of D.

eg:



sort itab by a b c.
loop at t_itab into f_itab.
at new a.
   f_itab-d = 1.
end at.
read table t_itab into f_itab1      "<-- different work area.
    with key a = f_itab-a  b = f_itab-b  c = f_itab-c binary search.
if sy-subrc is initial.
  add 1 to f_itab-d.
endif.
append f_itab to t_itab1.
clear f_itab.
delete t_itab index sy-tabix.

endloop.
" Now required unique entries are available in table t_itab1

Hope this helps you.

Regrads,

Sachinkumar Mehta.

Read only

Former Member
0 Likes
561

Hi,

Take one more internal table itab2.

move the contents of itab1 to itab2.

itab2[] = itab1[].

now go for LOOP.

data v_count type i.

loop at itab1.

clear v_count.

loop at itab2 where a = itab1-a and b = itab1-b and c = itab1-c and d = itab1-d.

v_count = v_count + 1.

itab2-d = v_count.

modify itab2.

endloop.

endloop.

Finally itab2 contains unique records.

Regards,

Kumar Bandanadham

Read only

Former Member
0 Likes
561

Hi Suker,

Please check the test program to apply the logic before updating the table.

DATA:
       l_itab TYPE TABLE OF ztest_str11,
       l_str  TYPE ztest_str11, " Structure with field1, field2...
       l_str_temp TYPE ztest_str11,
       l_count  TYPE i VALUE 1.

CLEAR l_str.
l_str-field1  = 12.
l_str-field2  = 11.
l_str-field3  = 1.
l_str-field4  = ''.
l_str-field5  = ''.
APPEND l_str TO l_itab.

CLEAR l_str.
l_str-field1  = 12.
l_str-field2  = 11.
l_str-field3  = 2.
l_str-field4  = ''.
l_str-field5  = ''.
APPEND l_str TO l_itab.

CLEAR l_str.
l_str-field1  = 12.
l_str-field2  = 11.
l_str-field3  = 1.
l_str-field4  = ''.
l_str-field5  = ''.
APPEND l_str TO l_itab.

CLEAR l_str.
l_str-field1  = 12.
l_str-field2  = 11.
l_str-field3  = 1.
l_str-field4  = ''.
l_str-field5  = ''.
APPEND l_str TO l_itab.

CLEAR l_str.
l_str-field1  = 12.
l_str-field2  = 11.
l_str-field3  = 2.
l_str-field4  = ''.
l_str-field5  = ''.
APPEND l_str TO l_itab.

CLEAR l_str.

SORT l_itab BY field1 field2 field3 field4.

LOOP AT l_itab INTO l_str.
  IF l_str_temp <> l_str.
    l_count = 1.
    l_str_temp = l_str.
    l_str-field4 = l_count.
    MODIFY l_itab FROM l_str.
  ELSE.
    l_count = l_count + 1.
    l_str-field4 = l_count.
    MODIFY l_itab FROM l_str.
  ENDIF.
  CLEAR l_str.
ENDLOOP.

After your internal table has the correct data, then update the entries in the table.

Regards,

George

Read only

Former Member
0 Likes
561

As long as A, B and C are not numerical fields and field D is, create an additional internal table with the same definition.

LOOP AT your_table INTO wa_your_table.

     wa_your_table-D = 1.
     COLLECT wa_your_table INTO a_new_table.

ENDLOOP.

As long as A, B and C are the same, the value of D will be incremented with 1 by the COLLECT statement.