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

SQL's WHERE clause priority order?

former_member202077
Participant
0 Likes
2,486

Hi Experts,

Example: My requirement is -- Select a record/sales order from VBAK where AUART = my_doc_type_1

if I do not found any record, then I have to look up as Select a record/sales order from VBAK where AUART = my_doc_type_2

So, am writing the SQL as below.

 SELECT vbeln INTO my_vbeln FROM vbak
WHERE auart IN ( my_doc_type_1, my_doc_type_2 ). 

Pls. let me know,

1) My assumption is that, system FIRST look for my_doc_type_1 record, if it does not found any such, then system will look for my_doc_type_2 record, am I correct?

2)if so, the PRIORITY is in the order in which we hv given in paranthesis?

3) Say, there are 2 records with same VBELN (I knew, its impossible having 2 recoords with same key!! just for example) and hving the AUART values as my_doc_type_1 and my_doc_type_2 like below

12345678 -


my_doc_type_1

12345678 -


my_doc_type_2

So, in this case system will select BOTH records?

Actually, i dont have SAP, so i can not do a quik test.

Thank you

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,056

Your select statement will pick all the records fall under the selection criteria.

In ur example it will pick both the records.

Guess there is no priority order in Where clause of SELECT statement.

SAP picks all the records satisfying the WHERE condition.

What you can do is :

1. Select where AUART = doc1.

2. if sy-subrc is not initial.

Select where AUART = doc2.

endif.

This is like if not found for doc1, check for doc 2.

Regards,

VR.

3 REPLIES 3
Read only

Former Member
0 Likes
1,057

Your select statement will pick all the records fall under the selection criteria.

In ur example it will pick both the records.

Guess there is no priority order in Where clause of SELECT statement.

SAP picks all the records satisfying the WHERE condition.

What you can do is :

1. Select where AUART = doc1.

2. if sy-subrc is not initial.

Select where AUART = doc2.

endif.

This is like if not found for doc1, check for doc 2.

Regards,

VR.

Read only

Former Member
0 Likes
1,056

Hi

This select query will fetch all the records from VBAK having order type as my_doc_type_1 or my_doc_type_2. This answers all your questions.

Read only

Former Member
0 Likes
1,056

Hi,

It's so funny that you said:

Actually, i dont have SAP, so i can not do a quik test.

So, with your current query:


 SELECT vbeln INTO my_vbeln FROM vbak
WHERE auart IN ( my_doc_type_1, my_doc_type_2 ).  "if i'm not wrong, you will get syntax error in this select query

It's always returns only one record. If you wanted to get all you must declare an internal table to hold your results:


 SELECT vbeln INTO TABLE my_itab FROM vbak
WHERE auart IN ( my_doc_type_1, my_doc_type_2 ).

REgards,