2013 Jun 18 4:39 PM
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!
2013 Jun 19 7:58 AM
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!
2013 Jun 19 7:58 AM
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.
2013 Jun 19 8:26 AM
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.
2013 Jun 19 8:51 AM
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.
2013 Jun 19 9:02 AM
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.
2013 Jun 19 9:59 AM
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>.
2013 Jun 19 12:44 PM
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!
2013 Jun 19 9:13 AM
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.
2013 Jun 19 10:04 AM
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.