And + Or in one Select-Statement

4.4k views Asked by At

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!

2

There are 2 answers

5
pixelarbeit On BEST ANSWER

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' )
0
Suncatcher On

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' ).