5

I'm trying to use this SELECT statement in ABAP:

  SELECT DISTINCT * FROM  dbtab
     INTO CORRESPONDING FIELDS OF TABLE itab
     WHERE  field1+7(16)  IN s_field1
     AND    field2        IN s_field2.

but I can't use offset for a dbtab column. How can I solve this problem?

I'm trying to avoid loop like

  SELECT DISTINCT * FROM  dbtab
     WHERE  field2        IN s_field2.
       IF field1+7(16)  IN s_field1
           ...
       endif.
  endselect.
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Darko
  • 1,448
  • 4
  • 27
  • 44
  • 2
    You are using ranges/select options in your selection, so you could try a contains pattern (CP) entry in the ranges field (Sorry, I have actual no SAP system available to create an example. My answer will follow when I have a system available - unless somebody else answers :) – knut Jan 15 '16 at 19:43
  • What do you select with `s_field1+7(16)`? A list of values or is it a complex selection (greater then, patterns...)? The solution in my comment would be (more or less) easy for ranges and single values. patterns etc. may become more difficult. – knut Jan 19 '16 at 09:52

2 Answers2

6

You can't use offset in OPEN SQL.

I would recommend to make a SELECT into an internal table and loop over it like this.

SELECT DISTINCT * FROM dbtab
INTO CORRESPONDING FIELDS OF TABLE itab
WHERE field2 IN s_field2.

LOOP AT dbtab into wa_itab.
  IF wa_itab-field1+7(16) IN s_field1
    ...
  ENDIF.
ENDLOOP.

On the other hand I would also define the internal table as SORTED or HASHED or if you prefer try to SORT itab by the field you are making the comparison. Field symbols could be an alternative also.

Hope it helps.

Nelson Miranda
  • 5,484
  • 5
  • 33
  • 54
1

If your policies allow you to you can use probably use an EXECUTE_SQL block instead, for example if you have an Oracle backend, which allows you to leverage native SQL constructs instead of just OPEN SQL. It's a judgement call whether this is justified or not depending on the performance gain. I think it's probably not.

You could also use a LIKE clause, which isn't as efficient as exactly knowing the offset, but would allow you to encapsulate each option with %: WHERE field 1 LIKE '%search_partial%' OR field1 LIKE ...

What is the use-case for this, it might be there's a more appropriate option if we have some context.

Lindsay Ryan
  • 433
  • 3
  • 9