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

Unable to loop across a dynamic table

Former Member
0 Likes
3,005

Hi everyone,

I'm unable to find a way to loop over a dynamic table with a dynamic where clause.

Here's what I'm trying to do:

1. Create a data reference as a sorted table of a DSO line type

2. Read data into that reference

3. create a dynamic where clause and put it into a string.

3. Trying to loop across it using that string in a where clause, that's where I get a CX_SY_ITAB_DYN_LOOP

I checked the string, the where-clause looks OK and all fields in the where clause are present in the data. Odd thing is, if I change the where-string to something random (such as "1=1"), it will still throw the same exception, but if I replace the dynamic table by a static one, the loop will run.

I didn't find anything in the documentation of LOOP saying that one couldn't loop across a dynamic table with a dynamic where clause...

For the sake of completeness, here's the relevant pieces of code (yes, it's the MM condition table):

<lp_matp_cond> TYPE SORTED TABLE

<lfs_data> TYPE ANY

I_MATP_COND TYPE REF TO DATA

CREATE DATA I_MATP_COND TYPE SORTED TABLE OF (dso_line_type) WITH UNIQUE DEFAULT KEY.

ASSIGN I_MATP_COND->* TO <lp_matp_cond>.

lv_matp_where = 'SOURSYSTEM=' && I_SOURSYSTEM && ' AND VENDOR=' && I_VENDOR &&

     ' AND MATERIAL=' && I_MATERIAL && ' AND PURCH_ORG=' && I_PURCH_ORG &&

     ' AND KAPPL=M AND INFO_TYPE=2'.

LOOP AT <lp_matp_cond> ASSIGNING <lfs_data> WHERE (lv_matp_where).

Your help is very much appreciated!

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,243

Operator '=' in 'where clause' of loop should have spaces before and after.

Something like this:

lv_matp_where = 'SOURSYSTEM = ' && I_SOURSYSTEM &&

                 ' AND VENDOR = ' && I_VENDOR &&

                 ' AND MATERIAL = ' && I_MATERIAL &&

                 ' AND PURCH_ORG = ' && I_PURCH_ORG &&

                 ' AND KAPPL = M AND INFO_TYPE = 2'.

This is the reason why your static loop with 'where clause' '1=1' does not work.

If loop still does not work, try getting a static loop to work with dynamic 'where clause' first.

You could also try TYPE ANY TABLE instead of TYPE SORTED TABLE.

Hi everyone,

I'm unable to find a way to loop over a dynamic table with a dynamic where clause.

Here's what I'm trying to do:

1. Create a data reference as a sorted table of a DSO line type

2. Read data into that reference

3. create a dynamic where clause and put it into a string.

3. Trying to loop across it using that string in a where clause, that's where I get a CX_SY_ITAB_DYN_LOOP

I checked the string, the where-clause looks OK and all fields in the where clause are present in the data. Odd thing is, if I change the where-string to something random (such as "1=1"), it will still throw the same exception, but if I replace the dynamic table by a static one, the loop will run.

I didn't find anything in the documentation of LOOP saying that one couldn't loop across a dynamic table with a dynamic where clause...

For the sake of completeness, here's the relevant pieces of code (yes, it's the MM condition table):

<lp_matp_cond> TYPE SORTED TABLE

<lfs_data> TYPE ANY

I_MATP_COND TYPE REF TO DATA

CREATE DATA I_MATP_COND TYPE SORTED TABLE OF (dso_line_type) WITH UNIQUE DEFAULT KEY.

ASSIGN I_MATP_COND->* TO <lp_matp_cond>.

lv_matp_where = 'SOURSYSTEM=' && I_SOURSYSTEM && ' AND VENDOR=' && I_VENDOR &&

     ' AND MATERIAL=' && I_MATERIAL && ' AND PURCH_ORG=' && I_PURCH_ORG &&

     ' AND KAPPL=M AND INFO_TYPE=2'.

LOOP AT <lp_matp_cond> ASSIGNING <lfs_data> WHERE (lv_matp_where).

Your help is very much appreciated!

8 REPLIES 8
Read only

Former Member
0 Likes
2,244

Operator '=' in 'where clause' of loop should have spaces before and after.

Something like this:

lv_matp_where = 'SOURSYSTEM = ' && I_SOURSYSTEM &&

                 ' AND VENDOR = ' && I_VENDOR &&

                 ' AND MATERIAL = ' && I_MATERIAL &&

                 ' AND PURCH_ORG = ' && I_PURCH_ORG &&

                 ' AND KAPPL = M AND INFO_TYPE = 2'.

This is the reason why your static loop with 'where clause' '1=1' does not work.

If loop still does not work, try getting a static loop to work with dynamic 'where clause' first.

You could also try TYPE ANY TABLE instead of TYPE SORTED TABLE.

Read only

0 Likes
2,243

Unfortunately changing to "1 = 1" doesn't help, it still throws the same exception, neither does changing it to a standard table. Static loop (= dummy static table) with dynamic "where" does work, but it's not really an option because I will need the added flexibility of the dynamic table creation.

Read only

0 Likes
2,243

It's your sample WHERE clause the definitive one?

I should approach from another point: do NOT use the WHERE clause of the LOOP sentence and add some CHECKs within it.

Read only

0 Likes
2,243

CHECK would probably be worth checking. But it's a speed tradeoff, the clause would only be matching very few records. Since I'm reading out of memory, it might not be that bad.

Read only

0 Likes
2,243

Left hand side of operator needs to be field present in internal table.

So instead of '1 = 1', 'INFO_TYPE = 2' would work.

Also, in condition KAPPL = M, here M is probably a literal, and not a variable. It needs to be enclosed in single quotes. I have enclosed that string in backticks so that quotes become part of string.

This 'where clause' should work.

lv_matp_where = 'SOURSYSTEM = ' && I_SOURSYSTEM &&

                ' AND VENDOR = ' && I_VENDOR &&

                ' AND MATERIAL = ' && I_MATERIAL &&

                ' AND PURCH_ORG = ' && I_PURCH_ORG &&

                ` AND KAPPL = 'M' AND INFO_TYPE = 2`.

Put a breakpoint at loop statement and verify that the fields in 'where clause' are actually present in <lfs_data>.

Read only

0 Likes
2,243

That was it, it seems to be very strict about formatting of the where clause (single quotes and spaces). The final clause is:

  lv_matp_where = 'SOURSYSTEM = ''' && I_SOURSYSTEM && ''' AND VENDOR = ''' && I_VENDOR &&
    ''' AND MATERIAL = ''' && I_MATERIAL && ''' AND PURCH_ORG = ''' && I_PURCH_ORG &&
    ''' AND KAPPL = ''M'' AND INFO_TYPE = 2'.

Thank you!

Read only

Former Member
0 Likes
2,243

Interesting...just discovered that doing it with another dynamically created table (1 column) doesn't give that error, so it somehow has to do with the table I'm passing.

Read only

0 Likes
2,243

If the table has only one field... which WHERE clause you can make?

Anyways, did you checked the resulting WHERE clause? I don't know if the && operator respect blanks, but I'm used to make my concatenations using CONCATENATE ... RESPECTING BLANKS.