3

In my select statement I chain different conditions with AND. Now I need for one condition an OR. How can I add this for just this one Attribute without affecting the other AND-statements before? That's my coding:

SELECT pernr reinr pdatv pdatb pdvrs abrec FROM PTRV_PERIO INTO CORRESPONDING FIELDS OF TABLE lt_ptrv_perio WHERE pdatv GE pa_begda AND pdatb LE pa_endda AND abrec EQ '2'.

For the last condition abrec EQ '2' I need an OR as well, like abrec EQ '2' OR '3'. How can I add this the best way without affecting the other ANDs?

Thanks for your hints!

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Dyrdek
  • 411
  • 4
  • 12
  • 33
  • 3
    parentheses ? `A and B and C and (D Or E)` – Charles Bretana Dec 27 '16 at 13:20
  • SELECT pernr reinr pdatv pdatb pdvrs abrec FROM PTRV_PERIO INTO CORRESPONDING FIELDS OF TABLE lt_ptrv_perio WHERE pdatv >=pa_begda AND pdatb <=pa_endda AND (abrec ='2' OR abrec ='3') – edt Dec 27 '16 at 13:21
  • Hey guys, thanks you for the quick response. I missed to say that I'm working in ABAP, sorry for that. Right now it doesn't looks like it works in ABAP like that. I still get errors with it. – Dyrdek Dec 27 '16 at 13:30

2 Answers2

3

Use parentheses:

EDIT: Seems like you need to add spaces after and before the brackets See this related question.

SELECT pernr reinr pdatv pdatb pdvrs abrec FROM PTRV_PERIO
INTO CORRESPONDING FIELDS OF TABLE lt_ptrv_perio
WHERE pdatv GE pa_begda
AND   pdatb LE pa_endda
AND   ( abrec EQ '2' OR abrec EQ '3' )
Community
  • 1
  • 1
pixelarbeit
  • 484
  • 2
  • 11
  • Hey guys, thanks you for the quick response. I missed to say that I'm working in ABAP, sorry for that. Right now it doesn't looks like it works in ABAP like that. I still get errors with it. – Dyrdek Dec 27 '16 at 13:30
  • 1
    @Suncatcher I found a related question. Check the updated answer. – pixelarbeit Dec 27 '16 at 17:53
  • It is **invalid** syntax! It is insufficient to find alike question and fill answer from there. Just RTFM. – Suncatcher Dec 27 '16 at 18:04
  • Sorry, I have mistaken you with Dyrdek and I didn't see that you've already provided the correct answer. – pixelarbeit Dec 27 '16 at 18:12
  • 1
    Hey, thanks for your help. It was really just the missing spaces. It works fine with your updated answer :) – Dyrdek Dec 28 '16 at 07:52
3

Use IN clause instead:

SELECT pernr reinr pdatv pdatb pdvrs abrec FROM PTRV_PERIO
INTO CORRESPONDING FIELDS OF TABLE lt_ptrv_perio
WHERE pdatv GE pa_begda
AND   pdatb LE pa_endda
AND   abrec IN ('2', '3').

The other valid alternative is:

SELECT pernr reinr pdatv pdatb pdvrs abrec FROM PTRV_PERIO
INTO CORRESPONDING FIELDS OF TABLE lt_ptrv_perio
WHERE pdatv GE pa_begda
AND   pdatb LE pa_endda
AND   ( abrec EQ '2' OR abrec EQ '3' ).
Suncatcher
  • 10,355
  • 10
  • 52
  • 90