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

Read table and if statements

Former Member
0 Likes
3,622

Hi experts,

I have 2 DSO containing information about some articles as following:

Table 1

Article

Exisiting

AAA

True

BBB

False

CCC

Table 2

Article

Exisiting

AAA

True

BBB

True

CCC

False

In table 1 I have to consolidate information upcoming from the 2 tables and compare the field existing with the following conditions:

For each article:

If one of the values in field existing for an article is initial, then take the value not initial (e.g Article AAA)

If the values contained for the same article are different in the 2 tables then take the value true (e.g Article BBB)

At the end my table 1 will be as this:

Article

Exisiting

AAA

True

BBB

True

CCC

False

I guess that I have to use a read table with key article statement, but I have issues with abap syntax.

Thanks for your support.

Amine

Hi experts,

I have 2 DSO containing information about some articles as following:

Table 1

Article

Exisiting

AAA

True

BBB

False

CCC

Table 2

Article

Exisiting

AAA

True

BBB

True

CCC

False

In table 1 I have to consolidate information upcoming from the 2 tables and compare the field existing with the following conditions:

For each article:

If one of the values in field existing for an article is initial, then take the value not initial (e.g Article AAA)

If the values contained for the same article are different in the 2 tables then take the value true (e.g Article BBB)

At the end my table 1 will be as this:

Article

Exisiting

AAA

True

BBB

True

CCC

False

I guess that I have to use a read table with key article statement, but I have issues with abap syntax.

Thanks for your support.

Amine

10 REPLIES 10
Read only

kakshat
Product and Topic Expert
Product and Topic Expert
0 Likes
2,045

LOOP AT table1 INTO struc1.

   IF struc1-existing = 'False' OR

      struc1-existing IS INITIAL.

    READ TABLE table2 INTO struc2 WITH KEY article = struc1-article.

    IF sy-subrc = 0.

       struc1-existing = struc2-existing.

    ENDIF.

   ENDIF.

ENDLOOP.

The above piece of code should do the trick.

Read only

Former Member
0 Likes
2,045

Thanks Kumar.

Amine

Read only

Former Member
0 Likes
2,045

Hi Kumar,

I will just add a sort statement by Article before the read table statement to have an efficient code.

Thanks.

Amine

Read only

0 Likes
2,045

if you made a sort  add a BINARY SEARCH option (for big table)

Fred

Read only

0 Likes
2,045

Hi Amine,

Since you are reading the table with a key, you would better create a hashed table with a unique key and then read  with table key.  The larger the table gets, the higher the performance gain for a hashed table.  The rule I use is quite simple :

  • Hashed table : If I have a unique key and need a single record
  • Sorted table : if I do a nested loop, if I don't have a unique key or if you need to perform a for all entries with the table (then also preferably a sorted table with unique key).
  • Standard table : if I don't need to read the table or loop over it with a where-clause, if the table is really small

I don't use binary search.  A sorted table works the same and if you forget to sort the table before you use binary search, your result might be wrong.  In case you need differents sorts on one table, use secondary indexex.

Regards,

Freek

Read only

FredericGirod
Active Contributor
0 Likes
2,045

Hi Amine,

buy a good Abap book, it's always very easy what you are asking. You will win time !

regards

Fred

Read only

0 Likes
2,045

Hi Frederic,

I learned too many intersting things in this forum

Amine

Read only

Former Member
0 Likes
2,045

Hello Amine,

        Please try this.

LOOP AT table1 INTO wa_table1 WHERE existing <> ‘true’.

  READ TABLE table2 INTO wa_table2 WITH KEY artical = wa_table1-artical.

  CHECK sy-subrc = 0

  AND   wa_table2-existing IS NOT INITIAL.

  wa_table1-existing = wa_table2-existing.

  MODIFY table1 FROM wa_table1.

ENDLOOP.

Regards

Read only

mayur_priyan
Active Participant
0 Likes
2,045

Hi,

I guess the following code will be more generic.

sort b by x.

loop at a into wa.

read table b into wb where x = wa-x binary search.

if sy-subrc = 0.

  wc-x = wb-x.

  if wb-y = wa-y.

     wc-y = wb-y.

  elseif wb-y NE wa-y.

     wc-y = 'TRUE'.

elseif NOT wb-y IS INITIAL and wa-y IS INITIAL.

     wc-y =   wb-y.

elseif NOT wa-y IS INITIAL and wb-y IS INITIAL.

    wc-y =   wa-y.

  endif.

APPEND wc To C.

endif.

Endloop.

Read only

Former Member
0 Likes
2,045

Hi

Select article

            existing

             from table1 into t_table1.

select article

           existing

            from table2 into t_table2

            for all entries in t_table1

            where article = t_table1-article.

loop at t_table1.

  here you can use the read statement and check whether the value initial and both exisisting values

are different and set accordingly.

endloop.