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/ loop through a dynamic internal table with a dynamic where clause

Former Member
0 Likes
2,156

Hi All,

We have a situation where we have two internal tables of similar type ( the tables are of dynamic type but we are at the part of the code where it is guaranteed that both the tables are of similar type ) . The table "A" contains the master details and table B contains partial details.

eg) Lets say table A has the following fields and data

FieldA FieldB FieldC FieldD FieldE

A1 B1 C1 D1 E1

A2 B2 C2 D2 E2

A3 B3 C3 D3 E3

A1 B2 C3 D4 E4

And table "B" has the following data ( fields are similar to table A )

FieldA FieldB FieldC FieldD FieldE

A1

C3

A2 B3

Each row of Table B acts as a "where clause" for the master table A.

The information required is the records of table A which satisfy the "condition" in each row of table B. Which would be the best way to approach it keeping the performance issue in mind ? In other words how to build a dynamic where clause to loop through Table A with the info available from Table B ?

Thanks and Regards,

Anil

A2 B3

1 ACCEPTED SOLUTION
Read only

matt
Active Contributor
0 Likes
1,559

So, you want sort of LOOP AT A WHERE fields match B.

Sort table B by the comparison fields. Find the first matching field in B, using the dynamic version of READ ... TRANSPORTING NO FIELDS. Store the sy-tabix of the found record.

Then

LOOP AT B FROM INDEX index.
  IF (fields don't match anymore).
    EXIT.
  ENDIF.
ENDLOOP.

matt

Hi All,

We have a situation where we have two internal tables of similar type ( the tables are of dynamic type but we are at the part of the code where it is guaranteed that both the tables are of similar type ) . The table "A" contains the master details and table B contains partial details.

eg) Lets say table A has the following fields and data

FieldA FieldB FieldC FieldD FieldE

A1 B1 C1 D1 E1

A2 B2 C2 D2 E2

A3 B3 C3 D3 E3

A1 B2 C3 D4 E4

And table "B" has the following data ( fields are similar to table A )

FieldA FieldB FieldC FieldD FieldE

A1

C3

A2 B3

Each row of Table B acts as a "where clause" for the master table A.

The information required is the records of table A which satisfy the "condition" in each row of table B. Which would be the best way to approach it keeping the performance issue in mind ? In other words how to build a dynamic where clause to loop through Table A with the info available from Table B ?

Thanks and Regards,

Anil

A2 B3

7 REPLIES 7
Read only

Former Member
0 Likes
1,559

Hi anil,

what u can do is loop at table B and then read tbale A. because if u loop at table A, then probably tbale has more rows than tbale B,and it take longer time. Do this way....

Remember always keep the less records loop outside....may be this will ease the peformance

LOOP AT B INTO WA_B

READ TABLE A WITH KEY FIELDA = WA_B-FIELDA

FIELDB = WA_B-FIELDB

INTO WA_A.

ENDLOOP.

You can in KEY use which fields you want to compare.

CASE 2:

if u are using loop then better use it in this way:

LOOP AT ITAB_A INTO WA_A.

LOOP AT ITAB_B INTO WA_B WHERE WA_B-FIELDA EQ WA_A-FIELDA.....

-


-


ENDLOOP.

ENDLOOP.

To be still more precise, sort the internal table on which you are performing a read statement, and then while using read table stmt, use it thru binary search. this will enhance ur performance..

Reward if useful......these are the 2 option u can go with...

Message was edited by: Sabah...

Sabahuddin Ahmed

Read only

0 Likes
1,559

Hi Sabahuddin,

I am working with Anil on the same issue. I am afraid the obvious solution suggested by you wont work because we have no idea about the "key" fields here. Remember they are dynamic and from what I can gather from the net there is no provision to dynamically build that where clause . ( the column field can be dynamic but that doesn't help here as the number of columns required is variable too )

Regards,

Read only

matt
Active Contributor
0 Likes
1,560

So, you want sort of LOOP AT A WHERE fields match B.

Sort table B by the comparison fields. Find the first matching field in B, using the dynamic version of READ ... TRANSPORTING NO FIELDS. Store the sy-tabix of the found record.

Then

LOOP AT B FROM INDEX index.
  IF (fields don't match anymore).
    EXIT.
  ENDIF.
ENDLOOP.

matt

Read only

Former Member
0 Likes
1,559

Hi Matthew,

Thanks for the response but am afraid it doesnt solve the problem. The nub of the issue is both the fields and the number of fields is dynamic here!! Which means have no idea about the comparison fields or even the number of them

I tried using the following statement :

loop at itab_B into wa_B .

Read table itab_A from wa_B into wa_a1 .

..if sy-subrc eq 0 do some processing

endloop.

In the above the structure "wa_B" contained values for some fields and the itab_A did contain records with the same data in wa_B in the some fields. I was under the impression that this statement would give the record which satisfies the condn in wa_B but that didnt happen . Am I missing something?

Regards,

Read only

matt
Active Contributor
0 Likes
1,559

I think you can get the names of the fields of the structures from RTTS.

( Or you could use the Internal Use only part of DESCRIBE ).

matt

Read only

Former Member
0 Likes
1,559

Yes we can get the fields alright but how does that help here? Can you please explain your approach in more detail? guess I must have missed something..

Read only

matt
Active Contributor
0 Likes
1,559

Well, now you've got the name of the fields to use in the read, the remaining problem is that the number might be different.

You can determine whether to use a field of the B table presumably by checking if it has a value.

So, once you know which fields in B have a value and their name, I think you have do something like.

CASE field_count.
  WHEN 1.
    READ TABLE A TRANSPORTING NO FIELDS WITH KEY (fld1) = <b_val_1>.
  WHEN 2.
    READ TABLE A TRANSPORTING NO FIELDS WITH KEY (fld1) = <b_val_1> (fld2) = <b_val_2>.
...

etc.

Not pretty, I'll admit.

Totally off the wall - but how about writing both datasets to transparent tables, and then using dynamic WHERE?

matt